在Heroku上部署create-react-app时出现无效的主机错误 [英] Invalid Host error when deploying a create-react-app on Heroku

查看:72
本文介绍了在Heroku上部署create-react-app时出现无效的主机错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Heroku上部署React应用程序时,我收到一个无效的主机错误。

When I deploy my React app on Heroku, I receive an Invalid Host error.

当我寻找原因时,我在文档中找到了它: https://github.com/facebook/create-react -app / blob / master / packages / react-scripts / template / README.md#invalid-host-header-errors-after-configuring-proxy

When I search for a reason, I find this in the docs: https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#invalid-host-header-errors-after-configuring-proxy

但是,当我创建一个.env文件并包含主机时,会出现此错误:

However, when I create an .env file and include the HOST, I receive this error:

Attempting to bind to HOST environment variable: XXX.herokuapp.com
If this was unintentional, check that you haven't mistakenly set it in your shell.

Could not find an open port at XXX.herokuapp.com.
Network error message: listen EADDRNOTAVAIL 52.205.227.252

我在做什么错了?

更新:我已经通过.env文件和Heroku CLI将HOST变量设置为Heroku url,但是仍然出现上述错误。


UPDATE: I've set the HOST variable to the Heroku url both via the .env file and through the Heroku CLI, but I'm still getting the above error.

推荐答案

这是一个有点老的问题,但是在处理同一问题时遇到了许多问题之一,因此我分享了我学到的东西,希望对其他人有用。

This is a slightly old question, but one of the many I came across while dealing with this same issue, so I'm sharing what I've learned and hope it will be useful to others.

一个可行的解决方案是使用http-proxy-middleware手动设置代理。请在此处查看说明:使用express后端将create-react-app部署到heroku会在浏览器中返回无效的主机头

A working solution is to use http-proxy-middleware to manually set up the proxy. Please see an explanation here: deploying create-react-app to heroku with express backend returns invalid host header in browser

这是一个最小的工作示例: https://github.com/sehailey/proxytest
,以下是部署的示例: https://proxytest2.herokuapp.com/

Here is a minimal working example: https://github.com/sehailey/proxytest and here is that example deployed: https://proxytest2.herokuapp.com/

这篇关于在Heroku上部署create-react-app时出现无效的主机错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆