我正在尝试使用flatiron构建一个小框架。我想使用nconf来加载我的所有配置文件,这样它们就可以在我的应用程序中的任何地方使用。在我的根目录中有我的app.js,我想把它从config/bootstrap.js中拉入配置数据。
config/config/js
module.exports =
{ 'app' :
{ "host" : "localhost"
, "port" : process.env.port || 3000
}
}bootstrap.js:
var nconf = require('nconf')
// database config
, dsource = require('./datasource')
// general or user config
, config = require('./config')
// allow overrides
nconf.overrides({
'always': 'be this value'
});
// add env vars and args
nconf.env().argv();
// load in configs from the config files
var defaults = {}
// so we can iterate over each config file
, confs = [dsource, config]
// for every config file
confs.forEach(function(conf)
{
// get each key
for (var key in conf)
{
// and add it to the defaults object
defaults[key] = conf[key]
}
})
// save the defaults object
nconf.defaults(defaults)
// logging this here works and properly shows the port setting
console.log('app port : ' + nconf.get('app:port'))
module.exports = nconf因此,当控制台从文件中登录时。一切似乎都加载得很好。但是当我尝试导出它,并从app.js请求它作为conf.get('app:port')时,它不起作用。
app.js (只是'flatiron create app.js‘中的一个普通应用)
var flatiron = require('flatiron')
, app = flatiron.app
, path = require('path')
, conf = require('./config/bootstrap')
app.config.file({ file: path.join(__dirname, 'config', 'config.json') });
app.use(flatiron.plugins.http);
app.router.get('/', function () {
this.res.json({ 'hello': 'world' })
});
// this doesnt work, conf
app.start(conf.get('app:port'));那么,我如何让它正常工作,以便在我的应用程序中的任何地方都可以使用config。理想情况下,我希望能够从像app.config这样的东西从任何地方获得配置
这是使用nconf的最佳方式吗?我似乎找不到很多例子。我看到的所有这些都只是从实际的nconfig示例文件中提取配置信息。而不是以app.config的身份从文件之外的任何位置
或者我没有正确地使用它?有没有更好的方法。理想情况下,我想使用这个引导程序文件来加载我所有的配置,以及资源/视图(RVP风格的应用程序),这样它就都加载了。
这是我对布局的总体想法,对于一个想法
|-- conf/
| |-- bootstrap.js
| |-- config.js
|-- resources
| |-- creature.js
|-- views/
|-- presenters/
|-- app.js
|-- package.json发布于 2013-02-19 18:33:59
你的配置可以在你可以访问应用的任何地方使用,如下所示:
app.config.get('google-maps-api-key')如果您像这样加载它:
app.config.file({ file: path.join(__dirname, 'config', 'config.json') })发布于 2012-09-08 18:37:50
这是加载JSON配置的正确方法:
nconf.use('file', {
file: process.cwd() + '/config.ini'
, format: nconf.formats.json
});https://stackoverflow.com/questions/12326644
复制相似问题