1 回答

TA貢獻1906條經驗 獲得超3個贊
查看您的 server.js 存儲庫后,您將所有到達服務器的流量(甚至是您自己的 api 請求)發(fā)送到前端。
首先確保您的服務器端路由以可區(qū)分的內容開頭,例如
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')
);
});
}
添加回答
舉報