forgeplus-react-v2/server/index.js

62 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import express from "express";
import path from "path";
import { getDomObj } from "./window"
import { render } from "./render";
import { url, zoneUrl } from "../src/ssrUrl";
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
app.use('/build', express.static('build'));
const options = (target) => {
return {
target: target,
changeOrigin: false, // 允许在请求头中更改主机
timeout: 30000,
};
}
const proxy = createProxyMiddleware(options('http://localhost:8081'));
// 设置代理
// app.use(['/api/zone', '/api/cms'], (req, res) => {
// proxy(req, res);
// });
// 设置代理
app.use(['/api', '/images', '/system', '/favicon', '/robots.txt', '/sitemap*.xml', '/sitemap*.txt', '/favicon.ico', '/admins', '/assets', '/attachments', '/oauth'], (req, res) => {
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.use(express.static(path.resolve(__dirname, '/home/pdl/forgeplus/public')));
// 匹配路径匹配到的返回处理后的html没匹配到转发到后端
app.get('*',async function (req,res) {
global.domObj = getDomObj()
const renderHead = await render(req,res)
if (!renderHead) {
proxy(req, res);
}
})
const port = 3000
console.log(`\n==> 🌎 Listening on port ${port}. Open up http://localhost:${port}/ in your browser.\n`)
app.listen(port, '0.0.0.0');