我正在build
使用Express服务提供一个create-react-app,并index.html
在服务之前附加一些脚本标签。由于我是index.html
在投放之前处理文件的,因此我不希望我的express.static
中间件处理对/
或的请求/index.html
。这是我试图使此工作:
app.use(
[/^\/index\.html/, /^\//],
express.static(path.join(__dirname, "../../build"))
);
app.get("/*", function (req, res) {
// logic to manipulate index.html
res.send($.html());
});
但是,即使插入了原始的html代码,这也给了我空白的屏幕,所以我假设静态中间件仍在处理/
请求,它只是停止提供其他静态资产。
如何配置此代码,以便我的express.static停止覆盖我的/*
路线?
保留路由很重要,/*
因为我希望它能够处理客户端应用程序中react-router中定义的所有路由。因此,我不能将express.static
中间件移动到该/*
路线的下方,否则它将覆盖对静态资产的所有请求。
你可以简单地添加一个单独的处理程序index.html
和/
静态中间件之前:
function serveIndex(res) {
res.send($.html());
}
app.get(["/index.html", "/"], (req, res) => {
serveIndex(res);
});
app.use(express.static(path.join(__dirname, "../../build")));
app.get("/*", (req, res) => {
serveIndex(res);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句