2017-04-22 69 views
0

我无法弄清楚如何反应路由器和高速路由一起工作。反应路由器和快速GET冲突

我有这个

app.get('*', function(req, res) { 
    res.sendFile(path.resolve(__dirname) + '/server/static/index.html'); 
}); 

// routes 
const apiRoutes = require('./server/routes/api'); 
app.use('/api', apiRoutes); 

问题是我的API无法使用GET,因为它会重定向到index.html的。如果我删除通配符路由,那么react-router将无法正常工作。

+2

只是改变顺序。首先放置你的'api'路线,然后放上'*'路线 – Panther

回答

2

app.get('*')语句将匹配任何请求在未来您可以轻松地改变语句的顺序解决您的问题。

// routes 
const apiRoutes = require('./server/routes/api'); 
app.use('/api', apiRoutes); 

app.get('*', function(req, res) { 
    res.sendFile(path.resolve(__dirname) + '/server/static/index.html'); 
}); 

这样一来,任何请求其路径的开始与/api将处理您的apiRoutes路由器,其他所有路由器都由星号处理。