【发布时间】:2016-07-03 07:58:08
【问题描述】:
我以 root 身份使用 react 路由器,“/”下的所有请求都被定向到 react 路由器。并且当 react router 发现 url 与任何定义的组件都不匹配时,它会使用 NoMatch 组件进行渲染。问题来了,NoMatch 被渲染,这就是我想要的,但状态码仍然是 200 而不是 404。当我的 css 或 js 文件被放置在错误的 url 上时,react router 会做同样的事情,它会响应 200 !然后页面告诉我我的资源内容类型有问题!
那么,我怎样才能使用 react router 来处理“/”中的所有内容,并且仍然让它正确处理 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'));
服务器端
router.use('/', function(req, res, next) {
res.render('index-react', {
title: 'some title'
});
});
【问题讨论】:
-
你已经想到了什么?分享一些代码
-
现在提供代码
-
我添加了那个道具,但是反应路由器没有处理这个,它不会发出状态 404 错误。是否可以通过 javascript 发出 404 状态错误?
标签: javascript reactjs http-status-code-404 frontend react-router