在 Heroku 上为 node.js 服务器设置端口
Setting the port for node.js server on Heroku
我启动了一个 node.js 服务器,使用以下行来设置端口:
app.set('port', process.env.PORT || 8080);
这意味着,它应该读取 PORT 环境变量或默认为 8080,就像它在本地 运行 时所做的那样。它们都不会发生在 Heroku 上,并且服务器始终使用默认端口 80。知道如何更改它吗?
heroku config
PORT: 8080
你不能。 Heroku 设置您应该绑定的 PORT 变量,并监听 tcp/80.
Heroku 像对待任何其他应用程序一样对待网络应用程序,并且不允许您直接分配侦听端口。 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. Each web process simply binds to a port,
and listens for requests coming in on that port. The port to bind to
is assigned by Heroku as the PORT environment variable.
The contract with Heroku is for the process to bind to a port to serve
requests. Heroku’s routers are then responsible for directing HTTP
requests to the process on the right port.
你应该像这样使用heroku打开的端口:
port = process.env.PORT || 80
如果尚未设置端口,则将其设置为 80
在我的例子中,heroku 正在侦听默认的 HTTPS 端口:443
并且通过 heroku config:get PORT
.
不可见
例如你可以这样做:
const PORT = process.env.PORT || 5001;
app.listen(PORT, () => console.log(`Server is listening on port ${PORT}...`));
所有答案都很棒!想稍微触及理论,以便人们理解为什么 Heroku 将其端口设置为 80 或 443。
计算机必须创建一个 约定 才能使用不同的协议相互通信。例如 HTTP、HTTPS、SSH、FTP 等
因此,达成协议,计算机将使用端口 80 进行 HTTP 通信,https
将使用端口 443 进行通信,依此类推。下面是一个table显示所有协议的常规保留端口号。
现在有些人可能会想,如果这些是保留端口号,我的计算机怎么让我使用端口号 80 和 443(例如 localhost:80)。您可以使用任何端口号(实际上您最多可以选择 65,535 个端口号)但是一旦您想要部署到现场并希望其他人使用您的应用程序那么您将不得不开始使用端口 80 (HTTP) 或端口的约定443 (https
).
Heroku 提供一个环境变量 process.env.PORT
来申请正确的常规端口号,以便其他人可以访问您的应用程序,让您轻松愉快。
80 是默认端口,因此请使用 80 而不是 3000 或 8000
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);
});
我启动了一个 node.js 服务器,使用以下行来设置端口:
app.set('port', process.env.PORT || 8080);
这意味着,它应该读取 PORT 环境变量或默认为 8080,就像它在本地 运行 时所做的那样。它们都不会发生在 Heroku 上,并且服务器始终使用默认端口 80。知道如何更改它吗?
heroku config
PORT: 8080
你不能。 Heroku 设置您应该绑定的 PORT 变量,并监听 tcp/80.
Heroku 像对待任何其他应用程序一样对待网络应用程序,并且不允许您直接分配侦听端口。 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. Each web process simply binds to a port, and listens for requests coming in on that port. The port to bind to is assigned by Heroku as the PORT environment variable.
The contract with Heroku is for the process to bind to a port to serve requests. Heroku’s routers are then responsible for directing HTTP requests to the process on the right port.
你应该像这样使用heroku打开的端口:
port = process.env.PORT || 80
如果尚未设置端口,则将其设置为 80
在我的例子中,heroku 正在侦听默认的 HTTPS 端口:443
并且通过 heroku config:get PORT
.
例如你可以这样做:
const PORT = process.env.PORT || 5001;
app.listen(PORT, () => console.log(`Server is listening on port ${PORT}...`));
所有答案都很棒!想稍微触及理论,以便人们理解为什么 Heroku 将其端口设置为 80 或 443。
计算机必须创建一个 约定 才能使用不同的协议相互通信。例如 HTTP、HTTPS、SSH、FTP 等
因此,达成协议,计算机将使用端口 80 进行 HTTP 通信,https
将使用端口 443 进行通信,依此类推。下面是一个table显示所有协议的常规保留端口号。
现在有些人可能会想,如果这些是保留端口号,我的计算机怎么让我使用端口号 80 和 443(例如 localhost:80)。您可以使用任何端口号(实际上您最多可以选择 65,535 个端口号)但是一旦您想要部署到现场并希望其他人使用您的应用程序那么您将不得不开始使用端口 80 (HTTP) 或端口的约定443 (https
).
Heroku 提供一个环境变量 process.env.PORT
来申请正确的常规端口号,以便其他人可以访问您的应用程序,让您轻松愉快。
80 是默认端口,因此请使用 80 而不是 3000 或 8000
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);
});