what is process.env.PORT || 3000
used for in Node.js? I saw this somewhere:
什么是process.env。端口|| 3000在Node.js中使用。我看到这个地方:
app.set('port', process.env.PORT || 3000);
If it is used to set 3000
as the listening port, can I use this instead?
如果用3000作为监听端口,我可以用这个吗?
app.listen(3000);
If not why?
如果不是为什么?
4 个解决方案
#1
155
In many environments (e.g. Heroku), and as a convention, you can set the environment variable PORT
to tell your web server what port to listen on.
在许多环境中(例如Heroku),作为一种约定,您可以设置环境变量端口来告诉web服务器监听哪个端口。
So process.env.PORT || 3000
means: whatever is in the environment variable PORT, or 3000 if there's nothing there.
所以process.env。|| 3000的意思是:无论环境变量端口是什么,或者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 3000
hard-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,这可能只针对您,也可能不针对您,这取决于您的需求和运行服务器的环境的需求。
#2
44
-
if you run
node index.js
,Node will use3000
如果运行节点索引。js,节点将使用3000。
-
If you run
PORT=4444 node index.js
, Node will useprocess.env.PORT
which equals to4444
in this example. Run withsudo
for ports below 1024.如果运行端口=4444节点索引。Node将使用process.env。端口在这个例子中等于4444。对1024以下的端口运行sudo。
#3
20
When hosting your application on another service (like Heroku, Nodejitsu, and AWS), your host may independently configure the process.env.PORT
variable for you; after all, your script runs in their environment.
当在另一个服务(如Heroku、Nodejitsu和AWS)上托管应用程序时,主机可以独立配置process.env。为你端口变量;毕竟,您的脚本在它们的环境中运行。
Amazon's Elastic Beanstalk does this. If you try to set a static port value like 3000
instead of process.env.PORT || 3000
where 3000 is your static setting, then your application will result in a 500 gateway error because Amazon is configuring the port for you.
亚马逊的弹性豆茎可以做到这一点。如果您试图将静态端口值设置为3000而不是process.env。端口|| 3000,其中3000是您的静态设置,那么您的应用程序将导致500个网关错误,因为Amazon正在为您配置端口。
This is a minimal Express application that will deploy on Amazon's Elastic Beanstalk:
这是一个将部署在Amazon的弹性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);
#4
0
In some scenarios, port
can only be designated by the environment and is saved in a user environment variable. Below is how node.js apps work with it.
在某些场景中,端口只能由环境指定,并保存在用户环境变量中。下面是如何节点。js应用程序可以使用它。
The process
object 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()
.
流程对象是提供关于当前节点的信息和控制的全局对象。js的过程。作为一个全局变量,节点总是可用的。不使用require()的js应用程序。
The process.env
property returns an object containing the user environment.
这个过程。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
js:从node app中读取新的环境变量
console.log(process.env.MY_TEST_PORT)
terminal: run the node app and get the value
终端:运行节点app获取值
$ node app.js
9999
#1
155
In many environments (e.g. Heroku), and as a convention, you can set the environment variable PORT
to tell your web server what port to listen on.
在许多环境中(例如Heroku),作为一种约定,您可以设置环境变量端口来告诉web服务器监听哪个端口。
So process.env.PORT || 3000
means: whatever is in the environment variable PORT, or 3000 if there's nothing there.
所以process.env。|| 3000的意思是:无论环境变量端口是什么,或者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 3000
hard-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,这可能只针对您,也可能不针对您,这取决于您的需求和运行服务器的环境的需求。
#2
44
-
if you run
node index.js
,Node will use3000
如果运行节点索引。js,节点将使用3000。
-
If you run
PORT=4444 node index.js
, Node will useprocess.env.PORT
which equals to4444
in this example. Run withsudo
for ports below 1024.如果运行端口=4444节点索引。Node将使用process.env。端口在这个例子中等于4444。对1024以下的端口运行sudo。
#3
20
When hosting your application on another service (like Heroku, Nodejitsu, and AWS), your host may independently configure the process.env.PORT
variable for you; after all, your script runs in their environment.
当在另一个服务(如Heroku、Nodejitsu和AWS)上托管应用程序时,主机可以独立配置process.env。为你端口变量;毕竟,您的脚本在它们的环境中运行。
Amazon's Elastic Beanstalk does this. If you try to set a static port value like 3000
instead of process.env.PORT || 3000
where 3000 is your static setting, then your application will result in a 500 gateway error because Amazon is configuring the port for you.
亚马逊的弹性豆茎可以做到这一点。如果您试图将静态端口值设置为3000而不是process.env。端口|| 3000,其中3000是您的静态设置,那么您的应用程序将导致500个网关错误,因为Amazon正在为您配置端口。
This is a minimal Express application that will deploy on Amazon's Elastic Beanstalk:
这是一个将部署在Amazon的弹性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);
#4
0
In some scenarios, port
can only be designated by the environment and is saved in a user environment variable. Below is how node.js apps work with it.
在某些场景中,端口只能由环境指定,并保存在用户环境变量中。下面是如何节点。js应用程序可以使用它。
The process
object 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()
.
流程对象是提供关于当前节点的信息和控制的全局对象。js的过程。作为一个全局变量,节点总是可用的。不使用require()的js应用程序。
The process.env
property returns an object containing the user environment.
这个过程。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
js:从node app中读取新的环境变量
console.log(process.env.MY_TEST_PORT)
terminal: run the node app and get the value
终端:运行节点app获取值
$ node app.js
9999