[Node] Using dotenv to config env variables

Install:

npm install dotenv --save

For example, we can store the sensitive information or env related information in a 'veriables.env' file:

// variables.env

NODE_ENV=development
DATABASE=mongodb://xxxxx
MAIL_USER=xx
MAIL_PASS=xx
MAIL_HOST=xxx.io
MAIL_PORT=2525
PORT=7777
MAP_KEY=xxxx
SECRET=xxx
KEY=xxx

Then we can use dotenv to load those configurations:

// import environmental variables from our variables.env file
require('dotenv').config({ path: 'variables.env' });

Now all the env related information will be stored in:

process.env
原文地址:https://www.cnblogs.com/Answer1215/p/6954384.html