简体   繁体   English

在 Heroku 上设置 node.js 服务器的端口

[英]Setting the port for node.js server on Heroku

I launched a node.js server with the following line to set the port:我使用以下行启动了一个 node.js 服务器来设置端口:

app.set('port', process.env.PORT || 8080);

This means that, it should either read the PORT env variable or default to 8080, as it does when it's run locally.这意味着,它应该读取 PORT 环境变量或默认为 8080,就像它在本地运行时一样。 Neither of them is happening on Heroku, and the server always uses the default port 80. Any idea how to change it? Heroku 上两者都没有发生,并且服务器始终使用默认端口 80。知道如何更改它吗?

heroku config
PORT: 8080

You can't.你不能。 Heroku sets the PORT variable that you are supposed to bind, and listens on tcp/80. Heroku 设置您应该绑定的 PORT 变量,并在 tcp/80 上进行侦听。

You should use the port opened by heroku like so:您应该像这样使用 heroku 打开的端口:

port = process.env.PORT || 80

It sets the port to 80 if it has somehow not been set already如果尚未设置端口,则它将端口设置为 80

All the answers are great!所有的答案都很棒! Wanted to touch on the theory a bit so people can understand why Heroku sets it port to 80 or 443.想稍微谈谈这个理论,这样人们就可以理解为什么 Heroku 将其端口设置为 80 或 443。

Computers had to create a convention to communicate with each other with a different protocol.计算机必须创建一个约定以使用不同的协议相互通信。 For example HTTP, HTTPS, SSH, FTP , etc.例如 HTTP、HTTPS、SSH、FTP 等。

As a result, there was an agreement that computers will communicate HTTP with port 80, https will communicate on port 443, and so on with the other protocol.结果,有一个协议,计算机将使用 80 端口进行 HTTP 通信, https将使用 443 端口进行通信,依此类推。 Below is a table displaying all the protocol's conventional reserve port number.下表显示了所有协议的常规保留端口号。

在此处输入图像描述

Now some of you people may be thinking well if these are reserve port number how come my computer lets me use port number 80 and 443 (ex localhost:80).现在你们中的一些人可能会想,如果这些是保留端口号,我的计算机为什么让我使用端口号 80 和 443(例如 localhost:80)。 You can use any port number (in fact you can choose up to 65,535 port number) but once you want to deploy to live and want others to use your application then you will have to start using the convention of port 80 (HTTP) or port 443 ( https ).您可以使用任何端口号(实际上您最多可以选择 65,535 端口号)但是一旦您想要部署到 live 并希望其他人使用您的应用程序,那么您将不得不开始使用端口 80 (HTTP) 或端口的约定443( https )。

Heroku makes it nice and easy for you by providing an environment variable process.env.PORT to apply for the correct conventional port number so others can access your app. Heroku通过提供环境变量process.env.PORT来申请正确的常规端口号,以便其他人可以访问您的应用程序,从而为您提供便利。

Heroku treats web apps just like any other app and doesn't allow you to assign listening ports directly. Heroku 像对待任何其他应用程序一样对待 Web 应用程序,并且不允许您直接分配侦听端口。 Your web server will be assigned a dynamic port by Heroku but to ACCESS it, you will need to use the default port (80). Heroku 将为您的 Web 服务器分配一个动态端口,但要访问它,您需要使用默认端口 (80)。

On Heroku, apps are completely self-contained and do not rely on runtime injection of a webserver into the execution environment to create a web-facing service.在 Heroku 上,应用程序是完全独立的,不依赖于将 web 服务器运行时注入到执行环境中来创建面向 web 的服务。 Each web process simply binds to a port, and listens for requests coming in on that port.每个 Web 进程都简单地绑定到一个端口,并监听来自该端口的请求。 The port to bind to is assigned by Heroku as the PORT environment variable. Heroku 将要绑定的端口分配为 PORT 环境变量。

The contract with Heroku is for the process to bind to a port to serve requests.与 Heroku 签订的合同是让进程绑定到端口以服务请求。 Heroku's routers are then responsible for directing HTTP requests to the process on the right port. Heroku 的路由器然后负责将 HTTP 请求引导到正确端口上的进程。

Reference: Heroku Runtime Principles - Web Servers参考: Heroku 运行时原则 - Web 服务器

You can do for example:例如,您可以这样做:

const PORT = process.env.PORT || 5001;

app.listen(PORT, () => console.log(`Server is listening on port ${PORT}...`));

就我而言,heroku 正在侦听默认的 HTTPS 端口: 443 ,并且通过heroku config:get PORT看不到它。

80 is the default port so use 80 instead of 3000 or 8000 80是默认端口,因此请使用80而不是30008000

const PORT = process.env.PORT || 80;
var server = app.listen(PORT, function() {
    var host = server.address().address;
    var port = server.address().port;
    console.log("server is listening at http://%s:%s", host, port);
});

Different ports on Heroku can be used, as long as the main $PORT is also used.可以使用 Heroku 上的不同端口,只要同时使用主 $PORT 即可。 See https://stackoverflow.com/a/43911373/9646899https://stackoverflow.com/a/43911373/9646899

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

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