从属性文件加载环境属性

  • 安装属性阅读器:
npm install properties-reader --save
  • 创建目录 env 以存储属性文件:
mkdir env
  • 创建 environments.js
process.argv.forEach(function (val, index, array) {
    var arg = val.split("=");
    if (arg.length > 0) {
        if (arg[0] === 'env') {
            var env = require('./env/' + arg[1] + '.properties');
            module.exports = env;
        }
    }
});
  • 示例 development.properties 属性文件:
# Dev properties
[main]
# Application port to run the node server
app.port=8080

[database]
# Database connection to mysql
mysql.host=localhost
mysql.port=2500
...
  • 加载属性的示例用法:
var enviorment = require('./environments');
var PropertiesReader = require('properties-reader');
var properties = new PropertiesReader(enviorment);
   
var someVal = properties.get('main.app.port');
  • 启动快速服务器
npm start env=development

要么

npm start env=production