Node.js 中的 process.env.PORT 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18864677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is process.env.PORT in Node.js?
提问by user-S
what is process.env.PORT || 3000used for in Node.js? I saw this somewhere:
process.env.PORT || 3000Node.js 中的用途是什么?我在某处看到了这个:
app.set('port', process.env.PORT || 3000);
If it is used to set 3000as the listening port, can I use this instead?
如果用来设置3000监听端口,我可以用它代替吗?
app.listen(3000);
If not why?
如果不是为什么?
回答by Nitzan Shaked
In many environments (e.g. Heroku), and as a convention, you can set the environment variable PORTto tell your web server what port to listen on.
在许多环境(例如 Heroku)中,作为惯例,您可以设置环境变量PORT来告诉您的 Web 服务器要侦听的端口。
So process.env.PORT || 3000means: whatever is in the environment variable PORT, or 3000 if there's nothing there.
所以process.env.PORT || 3000意味着:环境变量 PORT 中的任何内容,如果那里什么都没有,则为 3000。
So you pass that app.listen, or to app.set('port', ...), and that makes your server be able to accept a parameter from the environment what port to listen on.
因此,您将app.listen, 或传递给app.set('port', ...),这使您的服务器能够从环境中接受一个参数来侦听哪个端口。
If you pass 3000hard-coded to app.listen(), you're always listening on port 3000, which might be just for you, or not, depending on your requirements and the requirements of the environment in which you're running your server.
如果您将3000硬编码传递给app.listen(),则您始终在侦听端口 3000,这可能只适合您,也可能不适合您,具体取决于您的要求和运行服务器的环境的要求。
回答by Abdennour TOUMI
if you run
node index.js,Node will use3000If you run
PORT=4444 node index.js, Node will useprocess.env.PORTwhich equals to4444in this example. Run withsudofor ports below 1024.
如果你运行
node index.js,Node 将使用3000如果您运行
PORT=4444 node index.js,Node 将在本示例中使用process.env.PORTwhich4444。sudo为低于 1024 的端口运行。
回答by ThisClark
When hosting your application on another service (like Heroku, Nodejitsu, and AWS), your host may independently configure the process.env.PORTvariable for you; after all, your script runs in their environment.
在其他服务(如 Heroku、Nodejitsu 和 AWS)上托管您的应用程序时,您的主机可能会独立process.env.PORT为您配置变量;毕竟,您的脚本在他们的环境中运行。
Amazon's Elastic Beanstalk does this. If you try to set a static port value like 3000instead of process.env.PORT || 3000where 3000 is your static setting, then your application will result in a 500 gateway error because Amazon is configuring the port for you.
亚马逊的 Elastic Beanstalk 可以做到这一点。如果您尝试设置静态端口值,3000而不是process.env.PORT || 30003000 是您的静态设置,那么您的应用程序将导致 500 网关错误,因为 Amazon 正在为您配置端口。
This is a minimal Express application that will deploy on Amazon's Elastic Beanstalk:
这是一个将部署在 Amazon 的 Elastic Beanstalk 上的最小 Express 应用程序:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
// use port 3000 unless there exists a preconfigured port
var port = process.env.port || 3000;
app.listen(port);
回答by themefield
In some scenarios, portcan only be designated by the environment and is saved in a user environment variable. Below is how node.js apps work with it.
在某些场景下,port只能由环境指定并保存在用户环境变量中。下面是 node.js 应用程序如何使用它。
The processobject is a global that provides information about, and control over, the current Node.js process. As a global, it is always available to Node.js applications without using require().
该process对象是一个全局对象,它提供有关当前 Node.js 进程的信息并对其进行控制。作为全局变量,它始终可用于 Node.js 应用程序,而无需使用require().
The process.envproperty returns an object containing the user environment.
该process.env属性返回一个包含用户环境的对象。
An example of this object looks like:
此对象的示例如下所示:
{
TERM: 'xterm-256color',
SHELL: '/usr/local/bin/bash',
USER: 'maciej',
PATH: '~/.bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin',
PWD: '/Users/maciej',
EDITOR: 'vim',
SHLVL: '1',
HOME: '/Users/maciej',
LOGNAME: 'maciej',
_: '/usr/local/bin/node'
}
For example,
例如,
terminal: set a new user environment variable, not permanently
终端:设置一个新的用户环境变量,不是永久的
export MY_TEST_PORT=9999
app.js: read the new environment variable from node app
app.js: 从 node app 读取新的环境变量
console.log(process.env.MY_TEST_PORT)
terminal: run the node app and get the value
终端:运行节点应用程序并获取值
$ node app.js
9999

