试图在heroku上部署React应用。应用程序的api在本地运行良好,但是当我部署到heroku时,该api无法正常工作

时间:2019-10-01 19:26:55

标签: reactjs heroku server create-react-app http-proxy-middleware

我在后端使用Node + Express。最初,我尝试在react client的package.json中使用“ proxy”:http://localhost:3001,但是当我尝试在heroku上打开网站时,这导致了 Invalid Host Header 消息。

我遵循了这些insturctions,然后引导我使用我所处的http-proxy-middleware。

这是我的setupProxy.js,它位于应用程序的/ src上。

const proxy = require("http-proxy-middleware");

module.exports = function(app) {
  app.use('/api/*', proxy({
    target: 'https://safe-crag-59591.herokuapp.com',
    changeOrigin: true,
    logLevel: 'debug',
    router: {
      'localhost:3000': 'http://localhost:3001'
    }
  }));
};

当我将其部署到heroku时,前端工作正常。但是,当我尝试进行任何api调用时,都会收到HTTP 400错误消息。请求标头看起来不错(例如,标头:https://safe-crag-59591.herokuapp.com/api/getAllProduct),但是当我尝试使用邮递员中的任何路由时,都会遇到相同的错误。

以下是heroku日志中的错误示例:

at=info method=GET path="/api/getAllProduct" host=safe-crag-59591.herokuapp.com request_id=e1455e5b-91fc-418b-a379-98af1012f3d7 fwd="72.207.123.175, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72, 54.146.242.72,54.146.242.72" dyno=web.1 connect=2ms service=4213ms status=400 bytes=2757 protocol=https

对此我还很陌生,所以我确定我想念一些东西,但是我觉得我已经尝试了一切。

编辑:我忘了在服务器运行时添加,它设置了代理,并在控制台中显示此消息: [HPM] Proxy created: / -> https://safe-crag-59591.herokuapp.com 当我认为应该基于我的setupProxy.js [HPM] Proxy created: /api -> https://safe-crag-59591.herokuapp.com 但这对我来说还是很新的。当代理设置控制台时,尝试设置setupProxy.js的其他方法就是显示“ / api”。

0 个答案:

没有答案