2016-06-09 146 views
1

我使用Express和React構建同構應用。我想使用具有固定路徑的系列url進行反應,如:hostname/admin/xxx,hostname/admin/yyyhostname/admin/zzzUrl在反應路由器中不匹配任何路由

在高速路由器是:

// in server.js file 
app.use('/admin', admin); 

// in admin.js router file 
router.get('*', (req, res) => { 
    match() // react-router's match method 
} 

,並在react-routerroutes文件是:

<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'/',但對於客戶端,它是'/admin'。我如何解決它,除了在Express中使用app.use('/', admin)

回答

0

我的最終解決方案是增加'/admin'location財產match()方法:

match({ 
    routes, 
    location: '/admin' + req.url 
    }, (error, redirectLocation, props) => { 
});