【问题标题】:Serving a react build version using nodejs express使用 nodejs express 提供 react 构建版本
【发布时间】:2021-07-13 20:29:00
【问题描述】:

我正在使用快速服务器为反应应用程序提供服务。我使用代理为 api 请求设置反应应用程序。现在我正在使用 Nodejs 为反应构建提供 404 错误。 API 路由不再有效。

"proxy": "http://localhost:5000/api/v1"

app.use(express.static('frontend/build'))
const path = require('path')
app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, 'frontend', 'build', 'index.html'))
})

【问题讨论】:

    标签: reactjs express proxy


    【解决方案1】:

    您的默认路由应该重定向到您的静态资产,而不是尝试为它们提供服务。

    app.use(express.static('frontend/build'))
    // register other routes here
    app.get('/hello', (req, res) => {
      res.send('hello world')
    })
    // Wildcard catch other routes
    app.get('*', (req, res) => {
      res.redirect('/frontend/build')
    })
    

    【讨论】:

    • 我现在只能发帖请求。我现在收到内部服务器错误
    • 事实上响应数据是html
    • 我更新了答案,以明确在哪里注册您的其他 api 路由
    猜你喜欢
    • 2018-03-08
    • 1970-01-01
    • 2012-11-07
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多