從屬性檔案載入環境屬性
- 安裝屬性閱讀器:
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