3 回答

TA貢獻1877條經(jīng)驗 獲得超6個贊
從Node v0.5.x開始,您可以要求JSON文件(引用此答案)
config.json:
{
"username" : "root",
"password" : "foot"
}
app.js:
var config = require('./config.json');
log_in(config.username, config.password);

TA貢獻1845條經(jīng)驗 獲得超8個贊
后來,我找到了一個很好的用于管理配置的Node.js模塊:nconf。
一個簡單的例子:
var nconf = require('nconf');
// First consider commandline arguments and environment variables, respectively.
nconf.argv().env();
// Then load configuration from a designated file.
nconf.file({ file: 'config.json' });
// Provide default values for settings not provided above.
nconf.defaults({
'http': {
'port': 1337
}
});
// Once this is in place, you can just use nconf.get to get your settings.
// So this would configure `myApp` to listen on port 1337 if the port
// has not been overridden by any of the three configuration inputs
// mentioned above.
myApp.listen(nconf.get('http:port'));
它還支持將設置存儲在Redis中,編寫配置文件,并具有相當可靠的API,并且作為Flatiron框架計劃的一部分,還得到了備受推崇的Node.js商店之一Nodejitsu的支持。相當適合未來。
在Github上查看nconf。
- 3 回答
- 0 關注
- 945 瀏覽
添加回答
舉報