我在后端使用 Node+Express。起初,我尝试在反应客户端的 package.json 中使用“代理”:http://localhost:3001 ,但是当我尝试在 heroku 上打开我的网站时,这导致了Invalid Host Header消息。
我遵循这些指令,然后引导我使用我所在的 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)但是当我尝试使用 Postman 中的任何路由时,我遇到了同样的错误。
以下是 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”。