1

BrowserHistory(react-router v4)返回404,historyApiFallback不起作用。BrowserHistory(react-router v4)返回404,historyApiFallback不起作用

所以,当我去http://localhost:3000/app我得到了404。我尝试了以下解决方案,没有用......任何想法?我也试过historyApiFallback设置为true和index.html中不使用相对路径bundle.js即/static/bundle.js

historyApiFallback doesn't work in Webpack dev server 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' }, 

回答

1

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

我将以下内容添加到了我的server.js中,它的工作原理!

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