我正在使用Express和React来构建一个同构的应用程序。我想对具有固定路径的系列网址进行反应,例如:hostname/admin/xxx
,hostname/admin/yyy
,hostname/admin/zzz
。
在Express中,路由器是:
// in server.js file
app.use('/admin', admin);
// in admin.js router file
router.get('*', (req, res) => {
match() // react-router's match method
}
并在react-router
的{{1}}文件中:
routes
当我访问<Route path='/' component={Admin}>
<Route path='child' component={Child} />
</Route>
时,服务器呈现可以完全匹配路由,但浏览器抛出错误:hostname/admin
。
如果我将Warning: [react-router] Location "/admin" did not match any routes
更改为
routes
服务器呈现无法匹配任何路由。
我认为问题是,对于服务器呈现,<Route path='/admin' component={Admin}>
<Route path='child' component={Child} />
</Route>
是path
,但对于客户端,它是'/'
。除了在Express中使用'/admin'
之外,我该如何解决?
答案 0 :(得分:0)
我的最终解决方案是在'/admin'
方法中将location
添加到match()
属性:
match({
routes,
location: '/admin' + req.url
}, (error, redirectLocation, props) => {
});