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

process.env.PORT || 3000表示:process.env.PORT表示手动设置的端口号。默认端口为3000。如果你没有手动设置它,那么它将监听3000。

app.set('port', process.env.PORT || 3000)或app.listen(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

在许多环境中(例如Heroku),作为一种惯例,你可以设置环境变量PORT来告诉你的web服务器监听哪个端口。

process.env.PORT || 3000的意思是:环境变量PORT中的任何东西,如果没有,则是3000。

所以你把它传递给app.listen,或者app.set('port',…),这使得你的服务器能够接受来自环境的“监听哪个端口”参数。

如果将3000硬编码传递给app.listen(),则始终在监听端口3000,这可能只适合您,也可能不适合,这取决于您的需求和运行服务器的环境的需求。

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