test node to produce different environments using different profiles of the config-lite

config-lite is a lightweight module reads the configuration file.
config-lite will load a different profile from the config directory under the current directory, depending on the implementation process environment variables (NODE_ENV) of.
If you do not set NODE_ENV, then read the default configuration file of default,
if you set NODE_ENV, will merge the specified configuration file and the default configuration file as configuration,
config-Lite support .js, .json, .node, .yml ,. yaml file suffix.
If the program start NODE_ENV = test, through require ( 'config-lite') in turn degraded find config / test.js, config / test.json, config / test.node, config / test.yml, config / test. yaml and combined default configuration;

1, a common node production project, the following directory structure
Here Insert Picture Description
2, the current directory using the command line: npm i config-lite --savemounting config-lite assembly
3, app.js add calling code, process.env.NODE_ENV view the current value of the environment

const config = require('config-lite')(__dirname)
//监听端口号
const port = config.PORT

4, package.json added

"scripts": {
    "start": "set NODE_ENV=production&& node app.js",
    "test": "set NODE_ENV=test&& node app.js",
    "prod": "set NODE_ENV=production&& node app.js"
  },

5, if the reading is less than NODE_ENV add environment variables in My Computer

Reference: https://segmentfault.com/a/1190000010099383

Guess you like

Origin blog.csdn.net/rui15111/article/details/85064083