简体   繁体   English

如何设置节点应用程序在heroku的静态端口号上运行?

[英]How do I set node app to run on a static port number on heroku?

my node app runs on port 8083 locally. 我的节点应用程序在本地端口8083上运行。 when I push to heroku, how can I configure the service to run on the same port? 当我推送到heroku时,如何配置服务以在同一端口上运行?

You cannot do it. 你做不到。 Your code should listen on the port that you have in the PORT environment variable passed to you by the Heroku server that you can access as process.env.PORT and Heroku will listen on the outside on port 80 for HTTP and 443 for HTTPS. 您的代码应侦听Heroku服务器传递给您的PORT环境变量中具有的端口,您可以作为process.env.PORT访问该端口,并且Heroku将侦听外部端口80上的HTTP和443侦听HTTPS。

See the docs: 参见文档:

In particular: 尤其是:

Correct example: 正确的例子:

// Get the port:
const PORT = process.env.PORT || 3000;
// Listen on the port:
app.listen(PORT, () => console.log('Listening on', PORT));

The default (3000 in this example) is for situations when you run it outside of Heroku (like for testing). 默认值(在此示例中为3000)适用于在Heroku之外运行它的情况(例如用于测试)。 When it is run on Heroku it should always listen on the port provided by Heroku. 在Heroku上运行时,应始终侦听Heroku提供的端口。 If it listens on some other port then Heroku will not proxy the traffic to your app correctly. 如果它在其他端口上侦听,则Heroku将无法正确将流量代理到您的应用程序。

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

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