【发布时间】:2020-11-22 03:49:52
【问题描述】:
重新加载页面后出现问题,服务器只返回json而不是该页面
我正在使用 React 并从 build 文件夹返回静态文件,还有 express 句柄路由,它仅在运行 localhost 时在生产模式下重现,一切正常
app.use('/auth', authRoutes);
app.use('/user', userRoutes);
app.use(['/dota2', '/csgo', '/lol'], generalRoutes);
if (process.env.REACT_APP_ENV === 'production') {
console.log('Production is running');
app.use('/', express.static(path.join(__dirname, '../build')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../build', 'index.html'));
});
}
有路线
const router = Router();
router.get('/live', liveMatches);
router.get('/upcoming', upcomingMatches);
router.get('/past', pastMatches);
router.get('/:matchId', getMatchById);
router.get('/opponents/:tournamentId', opponents);
router.post('/past-team-matches', pastTeamMatches);
您可以访问mySite,您会看到 json 作为结果,但如果您清除 URL 中的 matchId 并单击任何匹配项,页面将正常加载
还有react-router
<ServiceRoute
key={key}
path={['/dota2', '/csgo', '/lol']}
exact
access
component={Matches}
/>
<ServiceRoute
key={key}
path={['/dota2/:matchId', '/csgo/:matchId', '/lol/:matchId']}
exact
access
component={MatchInfo}
/>
【问题讨论】:
标签: node.js reactjs express react-router router