【问题标题】:Allowing expess to pass routes to react-router允许 expess 将路由传递给 react-router
【发布时间】:2016-03-03 05:00:00
【问题描述】:

我正在开发一个 express webpack react/react router 应用程序 (^2.0.0-rc5)。在 express 中,我使用 app.use(express.static(path.join(__dirname, 'dist'))); 将我的反应应用程序映射到一个 url。

当我在我的应用程序中转到 http://localhost:3001/ 时,它可以正常导航。然后当我点击一个按钮 Link 'ing to http://localhost:3001/diff-place 时,它会导航到新页面。

但是,当我直接去http://localhost:3001/diff-place 时,express 拦截了请求并发出 404。

我不知道如何解决这个问题,因此欢迎提出任何建议。

【问题讨论】:

    标签: express reactjs react-router


    【解决方案1】:

    在您的快速路由结束时,请确保您定义了一个 catch all 路由,该路由将为任何页面请求加载客户端,并确保将示例中的 index.html 替换为您的 html 入口点。届时 react-router 将启动。

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

    【讨论】:

      猜你喜欢
      • 2015-08-03
      • 2015-10-30
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 2018-05-19
      • 1970-01-01
      • 2015-12-30
      相关资源
      最近更新 更多