2016년 2월 27일 토요일

[Node.js] Node.js 환경변수 process.env.NODE_ENV 에 대한 이해 및 사용 / [Node.js] Understanding the Node.js and using environment variables process.env.NODE_ENV

[Node.js] Node.js 환경변수 process.env.NODE_ENV 에 대한 이해 및 사용
[Node.js] Understanding the Node.js and using environment variables process.env.NODE_ENV


process.env는 아래 공식적인 문서에 나와있듯이 유저 환경변수를 사용합니다.
process.env uses the user's environment variables as shown in the following official documents.

Note Links : https://nodejs.org/api/process.html#process_process_env



만약 유저 환경변수에 대해 잘 모른다면 아래 글을 참조 하세요.
If you see the following articles if you do not know about the user's environment variables.

[Linux, Mac] environment variable path / 환경변수 설정






1.한가지 변수만 사용할때.
1. When you use only one kinds of variables.


한가지 환경변수 이를테면 production / development 등의 분기로직만 사용한다면
아래 순서대로 실행합니다.
If you use only one environment variable, such as a branch of logic, such as production / development Run the following order.


우선 ~/.bash_profile의 환경변수를 추가하여줍니다.(전역변수로 할경우 /etc/profile 에 설정합니다)
Priority will add environment variables in ~ /.bash_profile. (If you set a global variable in the /etc/profile)









추가된 환경변수가 제대로 동작하는지 아래와 같이 source명령어로 컴파일후 echo로 확인해 봅니다.
Add the environment variable is how it works properly after compiling a source command as follows: Demonstrate to echo.

$ source .bash_profile
$ echo $NODE_ENV









node에서 주로 상용하는 파일명인 app.js를 만들고 아래와 같이 console.log를 이용해
콘솔에 출력하도록 작성한후 node로 실행시켜 봅니다.
The file name usually used to create a node in app.js using console.log as follows:
After creating the console to try to run as a node.













2.여러가지 변수를 사용할때
2. When you use a variety of variables



그러나 node에서 여러가지 환경변수들, 이를테면 암호화 복호화를 위한 키값을 위한 변수들을
추가 하다보면 환경변수가 늘어나게 되는데 이럴경우 아래와 같은 방법을 추천합니다.
In this case, however, there is a variety of environmental variables in the node, such as the variables are added to the encrypted decryption key value for the environment variables it look stretched recommend the following methods:





유저 홈 디렉터리에 .node_profile 파일을 생성한후 아래와 같이 편집합니다.
After you create a file in the user's home directory, edit .node_profile below.




.bash_profile을 열어 가장 하단에 아래 내용을 추가합니다.
Open the .bash_profile and add the following lines to the bottom.

source "$HOME/.bash_profile"





방금 작성한 .bash_profile을 아래 명령어로 현재 터미널에 적용시킵니다.
Written to apply to just the current terminal .bash_profile down command.


$ source .bash_profile



app.js 파일을열어 아래와 같이 방금 입력한 NODE_ENV와 SECURE_KEY를 출력하도록 작성합니다.
app.js open the file you just created for output and input one NODE_ENV SECURE_KEY below.









마지막으로 node로 실행해서 결과를 확인합니다.
Finally, we run a node to see the results.



댓글 없음:

댓글 쓰기