express

import express from "express";
import bodyParser from "body-parser";
import fs from "fs"
const HOSTNAME = "localhost"; //IP地址(可修改)
const PORT = 8060; //服务端口号(可修改)

const app = express();
// let fs = require("fs");

// 配置跨域
let allowCors = function (req, res, next) {
  res.header("Access-Control-Allow-Origin", req.headers.origin);
  res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
  res.header("Access-Control-Allow-Headers", "Content-Type");
  res.header("Access-Control-Allow-Credentials", "true");
  next();
};
app.use(allowCors); //使用跨域中间件

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get("/", (req, res) => {
  // console.log(process.env);
  // console.log(process.env.NODE_ENV);

  res.send("MAP-SERVER");
});

// stream 形式读取地图影像
app.get(/image/, (req, res) => {
  //   console.log(req.url);
  let filePath = `.${req.url}`; //相对路径
  let stream = fs.createReadStream(filePath);
  stream.on("error", () => {
    res.status(404).end();
  });
  // 响应的 HTTP 标头设置
  if (filePath.indexOf(".xml") !== -1) {
    res.set("Content-Type", "text/xml");  
  } else if (filePath.indexOf(".png") !== -1) {
    res.set("Content-Type", "image/png");
  } else {
  }
  stream.pipe(res);
});

// stream 形式读取地图地形
app.get(/China/, (req, res) => {
  //   console.log(req.url);
  let filePath = `.${req.url}`; //相对路径
  let stream = fs.createReadStream(filePath);
  stream.on("error", () => {
    res.status(404).end();
  });
  // 响应的 HTTP 标头设置
  if (filePath.indexOf(".json") !== -1) {
    res.set("Content-Type", "application/json");
  } else if (filePath.indexOf(".terrain") !== -1) {
    res.set("Content-Type", "application/octet-stream");
  } else {
  }
  stream.pipe(res);
});

app.listen(PORT, () => {
  console.log(`Server running at http://${HOSTNAME}:${PORT}/`);
});

posted @   甜瓜小旋风  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示