Abh*_*ngh 4 node.js express reactjs server-side-rendering create-react-app
我正在使用 Express 提供一个 create-react-app 服务build,并在提供服务之前附加一些脚本标签index.html。由于我index.html在服务之前操作文件,因此我不希望我的express.static中间件处理/or 的请求/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());
});
Run Code Online (Sandbox Code Playgroud)
但这只会给我一个空白的白屏,即使原始的 html 代码被插入,所以我假设静态中间件仍在处理请求/,它只是停止服务其他静态资产。
如何配置此代码以便我的express.static 停止覆盖我的/*路线?
保留路由很重要,/*因为我希望它能够处理客户端应用程序中的反应路由器中定义的所有路由。因此,我无法将express.static中间件移动到/*路由下方,否则这将覆盖对静态资产的所有请求。
您可以{index: false}作为 的第二个参数传递express.static,这将排除index.html从目录发送的内容build。
app.use(express.static(path.join(__dirname, "../../build"), {index: false}));
app.get("*", (req, res) => {
// logic to manipulate index.html
res.send($.html());
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1788 次 |
| 最近记录: |