forgeplus-react/server/index.js

44 lines
1.2 KiB
JavaScript

import express from "express";
import "./window"
import {render} from "./render";
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
app.use('/build', express.static('build'));
const targetServer = 'http://localhost:4000'; // Java 服务器的地址
const options = {
target: targetServer,
changeOrigin: true, // 允许在请求头中更改主机
timeout: 30000
};
// 设置代理
app.use(['/api', '/images', '/system', '/favicon'], (req, res) => {
const proxy = createProxyMiddleware(options);
proxy(req, res);
});
// 中间件,用于排除特定的路径
function excludePath(req, res, next) {
// 检查请求的路径是否需要被排除
if (req.path.includes('/build/')) {
// 如果是,直接结束请求,不调用下一个中间件或路由处理器
res.status(404).send('This path is excluded.');
} else {
// 如果不是,继续执行下一个中间件或路由处理器
next();
}
}
// 应用中间件到所有路由
app.use(excludePath);
app.get('*',function (req,res) {
render(req,res);
})
const port = 5000
console.log(`\n==> 🌎 Listening on port ${port}. Open up http://localhost:${port}/ in your browser.\n`)
app.listen(port);