简体   繁体   中英

How to use nconf with flatiron properly

I am trying to build a small framework using flatiron. I want to use nconf to load in all my configuration files so theyre available anywhere in my app. in my root directory I have my app.js, which i want to pull in the config data from 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

so when console logging from in the file. everything seems to load fine. But when I try to export it, and require it from app.js as conf.get('app:port') it doesnt work.

app.js (just a vanilla app.js from 'flatiron create app')

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'));

So how can I get this to work properly so config is available anywhere in my app. ideally i would like to be able to have the config available from anywhere from something like app.config

Is this the best way to use nconf? I cant seem to find many examples. all the ones i see are just pulling config info from inside the actual nconfig example file. not from outside the file anywhere as app.config

Or am i not using it properly? Is there a better way to do it. Ideally i want to use this bootstrap file to load in all my configs, as well as resources/views (RVP style app) so its all loaded up.

This is the general idea i have for a layout, for an idea

|-- conf/
|   |-- bootstrap.js
|   |-- config.js
|-- resources
|   |-- creature.js
|-- views/
|-- presenters/
|-- app.js
|-- package.json

Your config is available from everywhere you have access to app like this:

app.config.get('google-maps-api-key')

if you loaded it like this:

app.config.file({ file: path.join(__dirname, 'config', 'config.json') })

This is the right way to load a JSON config:

nconf.use('file', {
  file: process.cwd() + '/config.ini'
, format: nconf.formats.json
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM