process.env란 무엇입니까?Node.js의 포트?
뭐가process.env.PORT || 3000
Node.js에서 사용됩니까?어디선가 봤어요
app.set('port', process.env.PORT || 3000);
설정에 사용되는 경우3000
리스닝 포트로, 제가 대신 이것을 할 수 있습니까?
app.listen(3000);
그렇지 않다면 왜?
많은 환경(예: Heroku)에서 규칙적으로 환경 변수를 설정할 수 있습니다.PORT
수신할 포트를 웹 서버에 알려줍니다.
그렇게process.env.PORT || 3000
즉, 환경 변수 PORT에 있는 모든 것 또는 아무것도 없는 경우 3000을 의미합니다.
그래서 당신은 그것을 전달합니다.app.listen
또는 로app.set('port', ...)
그러면 서버가 환경에서 "수신 대기 포트" 매개 변수를 허용할 수 있습니다.
합격하면3000
에 하드 코딩된.app.listen()
항상 포트 3000을 수신합니다. 포트 3000은 서버를 실행 중인 환경의 요구사항과 요구사항에 따라 사용자 전용일 수도 있고 그렇지 않을 수도 있습니다.
뛰면
node index.js
,Node는 다음을 사용합니다.3000
실행하는 경우
PORT=4444 node index.js
노드가 사용합니다.process.env.PORT
에 해당하는 것4444
이 예에서는실행 대상sudo
1024 미만 포트의 경우.
Heroku, Nodejitsu 및 AWS와 같은 다른 서비스에서 애플리케이션을 호스팅하는 경우 호스트가 독립적으로process.env.PORT
변수입니다. 결국 스크립트는 해당 환경에서 실행됩니다.
아마존의 Elastic Beanstalk는 이것을 합니다.다음과 같은 정적 포트 값을 설정하려는 경우3000
대신에process.env.PORT || 3000
여기서 3000은 정적 설정입니다. Amazon이 포트를 구성하고 있기 때문에 응용 프로그램에서 500 게이트웨이 오류가 발생합니다.
이것은 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);
어떤 시나리오에서는,port
환경에서 지정할 수 있으며 사용자 환경 변수에 저장됩니다.아래는 node.js 앱이 작동하는 방법입니다.
그process
object는 현재 Node.js 프로세스에 대한 정보와 제어를 제공하는 글로벌입니다.글로벌하게 Node.js 응용 프로그램에서는 항상 를 사용하지 않고 사용할 수 있습니다.require()
.
그process.env
property는 사용자 환경을 포함하는 개체를 반환합니다.
이 개체의 예는 다음과 같습니다.
{
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: 노드 앱에서 새 환경 변수 읽기
console.log(process.env.MY_TEST_PORT)
터미널: 노드 앱을 실행하고 값을 가져옵니다.
$ node app.js
9999
Dotenv
.env 파일의 환경 변수를 process.env로 로드하는 제로 종속성 모듈입니다.코드와 별도로 환경에 구성을 저장하는 것은 12요소 앱 방법론을 기반으로 합니다.
npm과 함께
npm 설치 dotenv
또는 Yarn과 함께
실 추가 도트엔비
사용.
애플리케이션에서 가능한 한 빨리 dotenv를 요구하고 구성합니다.
required "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
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
사용자의 요구 사항과 서버가 실행 중인 환경의 요구 사항에 따라 사용자의 요구 사항이 다를 수 있습니다.
언급URL : https://stackoverflow.com/questions/18864677/what-is-process-env-port-in-node-js
'programing' 카테고리의 다른 글
git log'로 첫 번째 커밋을 표시하는 방법은 무엇입니까? (0) | 2023.05.21 |
---|---|
MS SQL Server 2008용 포트를 찾는 방법은 무엇입니까? (0) | 2023.05.21 |
Windows DLL 파일을 테스트하여 32비트인지 64비트인지 확인하려면 어떻게 해야 합니까? (0) | 2023.05.21 |
Xcode 프로젝트에 몇 줄의 코드가 있는지 어떻게 알 수 있습니까? (0) | 2023.05.21 |
ObjectId 함수에 대한 Node.js Mongoose.js 문자열 (0) | 2023.04.01 |