相关疑难解决方法(0)

如何让路由器响应404状态代码?

我正在使用反应路由器作为root,并且"/"下的所有请求都被定向到路由器.当react路由器发现url与任何已定义的组件不匹配时,它会使用NoMatch组件进行渲染.这就解决了问题,NoMatch是渲染的,这就是我想要的,但状态代码仍然是200而不是404.当我的css或js文件放置错误的url react路由器做同样的事情时,它会响应200 !然后该页面告诉我,我的资源内容类型存在一些问题!

那么,我如何使用react路由器来处理"/"中的所有内容并仍然可以正确处理404错误(以404状态代码响应)?

反应路由器中的代码

render((
  <Router history={browserHistory}>
    <Route path="/" component={App}>
      <IndexRoute component={Index}/>
      <Route path="archived" component={App}>
        <IndexRoute component={ArchivedPage}/>
        <Route path="project/:projectId" component={ArchivedDetailPage}/>
      </Route>
      <Route path="*" component={NoMatch}/>
    </Route>
  </Router>
), document.getElementById('app'));
Run Code Online (Sandbox Code Playgroud)

服务方面

  router.use('/', function(req, res, next) {
    res.render('index-react', {
      title: 'some title'
    });
  });
Run Code Online (Sandbox Code Playgroud)

javascript frontend http-status-code-404 reactjs react-router

22
推荐指数
4
解决办法
2万
查看次数