process.env.PORT || 3000
在 Node.js 中有什么用途?我在某个地方看到过这个:
app.set('port', process.env.PORT || 3000);
如果将端口号设为3000
,那我可不可以使用这个端口号呢?
app.listen(3000);
如果不是的话,为什么?
在许多环境中(例如Heroku),通常可以设置环境变量PORT
来告诉您的Web服务器要侦听的端口。
因此,process.env.PORT || 3000
的意思是:无论环境变量PORT中是什么,如果没有内容,则使用3000。
因此,将其传递给app.listen
或app.set('port', ...)
,这使得您的服务器能够从环境接受“要侦听哪个端口”的参数。
如果您在app.listen()
中硬编码3000
,则始终会侦听端口3000,这可能仅适用于您或不适用于您的要求和运行服务器的环境要求。
如果运行 node index.js
,Node将使用3000
如果运行 PORT=4444 node index.js
,Node将使用process.env.PORT
。在这个例子中,process.env.PORT
等于4444
。对于低于1024的端口,请使用sudo
运行。
当您将应用程序托管在其他服务(如Heroku、Nodejitsu和AWS)上时,您的主机可能会独立配置process.env.PORT
变量;毕竟,您的脚本在他们的环境中运行。
Amazon的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);
const {PORT = 3000} = process.env
。 - Julian在某些情况下,port
只能由环境指定,并保存在用户环境变量中。以下是 node.js 应用程序如何处理它。
process
对象是一个全局对象,提供有关当前 Node.js 进程的信息和控制。作为全局对象,它始终可在 Node.js 应用程序中使用,而无需使用 require()
。
process.env
属性返回一个包含用户环境的对象。
此对象的示例如下:
{
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: 从node应用程序读取新的环境变量
console.log(process.env.MY_TEST_PORT)
终端:运行节点应用程序并获取值
$ node app.js
9999
Dotenv
是一个零依赖的模块,可以将 .env 文件 中的环境变量加载到 process.env. 中。将配置与代码分离存储在环境中,是基于“十二要素应用程序”方法论的。
npm install dotenv
yarn add dotenv
用法
在应用程序尽早的阶段,引入并配置 dotenv。
require('dotenv').config()
在文件资源管理器中首先创建一个 .env 文件,并在其中写入:
PORT: 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
process.env.PORT || 3000
的意思是: process.env.PORT
是你手动设置的 PORT
号码。 3000
是默认的 port
。如果你没有手动设置,那么它将会监听 3000。
在你的代码中,app.set('port', process.env.PORT || 3000)
或者 app.listen(3000)
意思是一样的。它只是说它应该作为环境参数监听哪个端口。
3000
是你传递给 app.listen()
的硬编码参数,这意味着无论何时运行你的后端代码,你都将始终侦听 port 3000
,这可能符合你的要求,也可能不符合,这取决于你的要求和服务器运行的环境要求。