简体   繁体   English

为什么 heroku 在我的反应应用程序中不能与代理一起使用?

[英]Why won't heroku work with proxy in my react app?

I have a simple notes app that uses react(create react app) in a client folder with a node express mongoose back end.我有一个简单的笔记应用程序,它在客户端文件夹中使用 react(create react app) 和 node express mongoose 后端。 If I try to upload to heroku it rejects it because of the proxy ("proxy": " http://localhost:3001 ") in the package.json in the client folder. If I try to upload to heroku it rejects it because of the proxy ("proxy": " http://localhost:3001 ") in the package.json in the client folder. If I get rid of the proxy it will upload to heroku no problem but because it's not there it won't talk to the back end so nothing works.如果我摆脱代理,它将上传到 heroku 没问题,但因为它不在那里它不会与后端对话,所以没有任何效果。 When pushing to heroku with the proxy it will build but the app gives me "Invalid Host header".当使用代理推送到 heroku 时,它将构建,但应用程序给了我“无效的主机头”。 How do I go about fixing this?我该如何解决这个问题? All I have tried is deleting the proxy.我所尝试的只是删除代理。

Are you using a fixed port in your app?您是否在应用程序中使用固定端口? Heroku assigns you a dynamic port that you must bind to, so you can't simply use a fixed port like 3001. Heroku 为您分配一个必须绑定到的动态端口,因此您不能简单地使用像 3001 这样的固定端口。

You should bind to process.env.PORT when running on Heroku instead.在 Heroku 上运行时,您应该绑定到 process.env.PORT。 See runtime principles .请参阅运行时原则

There is a build pack that works around this problems and offer a better experience with create-react-app check it out:有一个构建包可以解决这个问题,并通过create-react-app提供更好的体验,请查看:

Just create your application by running:只需运行以下命令创建您的应用程序:

heroku create <app name> --buildpack mars/create-react-app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM