【问题标题】:Express doesn't load page only return jsonExpress 不加载页面只返回 json
【发布时间】: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 中的 ma​​tchId 并单击任何匹配项,页面将正常加载

还有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


    【解决方案1】:

    让我们按照 express 在这种情况下所做的路由匹配:

    1. 查找/dota2/566624时,会匹配到这里:app.use(['/dota2', '/csgo', '/lol'], generalRoutes);并返回JSON。
    2. 在查找 /dota2 时,它不会匹配 app.use(['/dota2', '/csgo', '/lol'], generalRoutes);,因此它将继续向下直到匹配 app.get('*', (req, res) =&gt; {,服务于 React 页面。

    我在这里看到的问题是,您对 API 和 React 上的前端路由使用了完全相同的路由。理想情况下,当从同一服务器提供 API 和前端应用程序时,您应该为 API 路由添加前缀,这样它们就不会与前端路由冲突。假设您为 API 路由添加前缀:/api。现在我们有了:

    app.use('/api/auth', authRoutes);
    app.use('/api/user', userRoutes);
    app.use(['/api/dota2', '/api/csgo', '/api/lol'], generalRoutes);
    
    // ... everything is the same down here
    
    1. /api/dota2/566624。将匹配在第一次加载时返回 JSON 的端点。
    2. /dota2/566624。将与 JSON 端点不匹配,并将遵循加载 React 应用程序的 '*' 路由,稍后,一旦加载应用程序,该路由将由您在 react 应用程序上使用的路由器处理。

    不要像我在上面的示例中那样添加前缀,也可以使用您一直用于此目的的const router = Router();。我对前缀进行了硬编码,以便让示例保持简短。

    最后一个问题是:为什么在开发过程中没有发生这种情况? 我没有所有信息,但据我所知,您正在从与运行 API 的服务器不同的服务器上为前端应用程序提供服务;在这种情况下,您不会遇到路由冲突,因为它们是来自不同端口的服务器,例如在 localhost:8080 上服务的前端和在localhost:5000 上服务的 API。

    我希望这会有所帮助!如果您需要信息,请发表评论!

    【讨论】:

    • 感谢您对我的问题感兴趣,我使用“proxy”:“localhost:6000” - 它正在运行我的服务器,来自在本地端口:3000 上运行的 react-app,我如您所写,已添加“api/”,现在我根本无法得到回复,也许您需要更多信息,请告诉我
    • 抱歉耽搁了...这很有趣...我从未在 CRA 配置中使用代理选项,但它转发任何不接受 text/html 的请求...采取查看文档create-react-app.dev/docs/proxying-api-requests-in-development。这意味着如果您通过 axios 发出请求,请确保请求仅将 te Accept 标头设置为 application/json
    • 谢谢先生,问题确实是因为前端的路由和后端的路由一样,添加前缀/api后它已经修复了
    猜你喜欢
    • 2014-08-12
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 2018-03-07
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多