process.env.PORT || 3000在Node.js中用于什么?我在什么地方看到过这个:

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

如果使用3000作为监听端口,我可以用这个代替吗?

app.listen(3000);

如果不是,为什么?


当前回答

Click to show 2 definitions.

(property) NodeJS.Process.env: NodeJS.ProcessEnv
The process.env property returns an object containing the user environment. See environ(7).

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'
}
It is possible to modify this object, but such modifications will not be reflected outside the Node.js process, or (unless explicitly requested) to other Worker threads. In other words, the following example would not work:

$ node -e 'process.env.foo = "bar"' && echo $foo
While the following will:

import { env } from 'process';

env.foo = 'bar';
console.log(env.foo);
Assigning a property on process.env will implicitly convert the value to a string. This behavior is deprecated. Future versions of Node.js may throw an error when the value is not a string, number, or boolean.

import { env } from 'process';

env.test = null;
console.log(env.test);
// => 'null'
env.test = undefined;
console.log(env.test);
// => 'undefined'
Use delete to delete a property from process.env.

import { env } from 'process';

env.TEST = 1;
delete env.TEST;
console.log(env.TEST);
// => undefined
On Windows operating systems, environment variables are case-insensitive.

import { env } from 'process';

env.TEST = 1;
console.log(env.test);
// => 1
Unless explicitly specified when creating a Worker instance, each Worker thread has its own copy of process.env, based on its parent thread’s process.env, or whatever was specified as the env option to the Worker constructor. Changes to process.env will not be visible across Worker threads, and only the main thread can make changes that are visible to the operating system or to native add-ons.

@since — v0.1.27

其他回答

在某些场景下,port只能由环境指定,保存在用户环境变量中。下面是node.js应用程序如何使用它。

process对象是一个全局变量,提供了关于当前Node.js进程的信息和控制。作为一个全局变量,它总是对Node.js应用程序可用,无需使用require()。

这个过程。属性返回一个包含用户环境的对象。

该对象的示例如下:

{
  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'
}

例如,

终端:设置一个新的用户环境变量,不是永久的

export MY_TEST_PORT=9999

app.js:从节点app中读取新的环境变量

console.log(process.env.MY_TEST_PORT)

终端:运行节点app获取值

$ node app.js
9999

如果你运行node index.js, node将使用3000 如果你运行PORT=4444节点索引.js, node将使用process.env.PORT,在本例中它等于4444。对于1024以下的端口,使用sudo运行。

Dotenvis是一个零依赖模块,它将环境变量从.env文件加载到process.env中。在与代码分开的环境中存储配置是基于十二因素应用程序方法的。

与npm

NPM安装dotenv

或用纱线

纱线加dotenv

使用

在应用程序中尽可能早地要求并配置dotenv。

要求(' dotenv ') . config ()

首先在文件资源管理器中创建一个.env文件,并在其中写入:

端口:8080

const http = require("http");

require("dotenv").config();

let port = process.env.PORT;
let host = process.env.HOST;

let server = http.createServer((req, res) => {
  console.log("Thanks for the request");
  res.writeHead(200, { "Content-Type": "text/plain" });
  res.end("You Rock");
});

server.listen(port, host, () => {
  console.log(`Server is listening ${host}:${port}`);
});
Click to show 2 definitions.

(property) NodeJS.Process.env: NodeJS.ProcessEnv
The process.env property returns an object containing the user environment. See environ(7).

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'
}
It is possible to modify this object, but such modifications will not be reflected outside the Node.js process, or (unless explicitly requested) to other Worker threads. In other words, the following example would not work:

$ node -e 'process.env.foo = "bar"' && echo $foo
While the following will:

import { env } from 'process';

env.foo = 'bar';
console.log(env.foo);
Assigning a property on process.env will implicitly convert the value to a string. This behavior is deprecated. Future versions of Node.js may throw an error when the value is not a string, number, or boolean.

import { env } from 'process';

env.test = null;
console.log(env.test);
// => 'null'
env.test = undefined;
console.log(env.test);
// => 'undefined'
Use delete to delete a property from process.env.

import { env } from 'process';

env.TEST = 1;
delete env.TEST;
console.log(env.TEST);
// => undefined
On Windows operating systems, environment variables are case-insensitive.

import { env } from 'process';

env.TEST = 1;
console.log(env.test);
// => 1
Unless explicitly specified when creating a Worker instance, each Worker thread has its own copy of process.env, based on its parent thread’s process.env, or whatever was specified as the env option to the Worker constructor. Changes to process.env will not be visible across Worker threads, and only the main thread can make changes that are visible to the operating system or to native add-ons.

@since — v0.1.27

当你的应用程序托管在另一个服务(如Heroku, Nodejitsu和AWS)上时,你的主机可以独立地为你配置process.env.PORT变量;毕竟,您的脚本在他们的环境中运行。

亚马逊的Elastic Beanstalk就做到了这一点。如果您试图设置一个静态端口值,例如3000,而不是process.env.PORT || 3000,其中3000是您的静态设置,那么您的应用程序将导致一个500网关错误,因为Amazon正在为您配置端口。

这是一个最小的Express应用程序,将部署在Amazon的Elastic Beanstalk上:

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);