1 回答
![?](http://img1.sycdn.imooc.com/54584f8f00019fc002200220-100-100.jpg)
TA贡献1906条经验 获得超3个赞
查看您的 server.js 存储库后,您将所有到达服务器的流量(甚至是您自己的 api 请求)发送到前端。
首先确保您的服务器端路由以可区分的内容开头,例如
app.get('/api/*',(req,res)=>/*somecode*/)
这是因为您的服务器会混淆诸如“/login”之类的东西,如果它也是您前端的一条路由,并且最终只会根据定义它们的时间来服务一个或另一个。
然后更新你的 server.js 以匹配它,它应该可以工作:
//API Requests handled first
require('./routes')(app);
//Non api requests in production
if (process.env.NODE_ENV === 'production') {
app.use([someProductionMiddleware()])
// Express will serve up production assets i.e. main.js
app.use(express.static('client/build'));
// If Express doesn't recognize route serve index.html
const path = require('path');
app.get('*', (req, res) => {
res.sendFile(
path.resolve(__dirname, 'client', 'build', 'index.html')
);
});
}
添加回答
举报