BrowserHistory (react-router v4) returns 404 和 historyApiFallback 不工作

BrowserHistory (react-router v4) returns 404 and historyApiFallback not working

BrowserHistory (react-router v4) returns 404 和 historyApiFallback 不工作。

所以当我去 http://localhost:3000/app 时,我得到了 404。我尝试了以下解决方案但无济于事...有什么想法吗?我还尝试将 historyApiFallback 设置为 true 并且不使用 bundle.js 的相对路径,即 index.html

中的 /static/bundle.js

historyApiFallback doesn't work, for scripts, if you're nested multiple path sections deep

Index.html

    `<script src="bundle.js"></script`>

Webpack 输出

output: {
 path: path.join(__dirname, 'public'),
 filename: 'bundle.js',
 publicPath: '/'
},

Webpack devServer

historyApiFallback: { index: 'public/index.html' },

令人尴尬的是,我没有正确设置我的 Express 服务器路由。我使用了基于 HashRouter 的教程中的相同设置....

我将以下内容添加到我的 server.js 中,它起作用了!

app.get('*', (req, res) => {
 res.sendFile(path.join(__dirname, 'public/index.html'));
});