# react-router v4 刷新出现找不到页面(NO FOUND)解决方案

react-router v4 刷新页面出现找不到问题解决方案
原因
### 浏览器被刷新相当于重新请求了服务端的 page 接口,当后端没有这个接口时,就没有document文档返回,这时url 并没有被js 观察处理

解决

如果是使用webpack-dev-server,请将 historyApiFallback: true 这个配置加入至 devServer 中. 以及在output 中配置 publicPath: ‘/’
如果是使用自定义的node服务器的话,需自己手写一个404接口. 将所有的url 都返回到index.html文档

实例

koa
const koaWebpack = require(‘koa-webpack’);

async startService() {
const middleware = await koaWebpack({ config: this.webpackConfig });

this.app.use(middleware);

app.use(async ctx => {
const filename = path.resolve(this.webpackConfig.output.path, ‘index.html’);
ctx.response.type = ‘html’;
ctx.response.body = middleware.devMiddleware.fileSystem.createReadStream(
filename
);
});

this.app.listen(this.port, () => {
console.log(`当前服务器已启动`, `http://${this.host}:${this.port}`);
});
}

[参考地址][1]

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理