In the following example we have settings for test
, development
and production
environments. defaults
is used where a value for that particular key is not supplied for the current environment.
{
"test": {
"url": "localhost"
},
"staging": {
"url": "staging.com"
},
"production": {
"url": "production.com",
"port": 80
},
"defaults": {
"port": 3000
}
}
// ./config/index.js
'use strict'
var auto_nconf = require('./auto_nconf')
// create main settings configuration
var settings = exports.settings = auto_nconf.createConfiguration('settings', __dirname)
// create additional configuration for 'types'
var types = auto_nconf.createConfiguration('types', __dirname)
// ./lib/somefile.js
'use strict'
var config = require('../config') // load our config
var settings = config.settings // access settings
db.connect(settings.db.hostname) // for example
Environment can be set explicitly by setting NODE_ENV
environment variable, or auto-detected.
Auto detection tries to determine if you are running inside a test suite, or running the app manually. If in test suite, use test
settings, if manually testing app, use development
settings.
Where this is useful is for cases where you want to have smaller, faster data in test mode than development mode. E.g. limit the number of items per page to a small number in test mode to improve test suite speed, while when testing manually, use more human-friendly numbers.
Recommended usage is to supply a default settings.defaults.json
in your config
folder,
and override any settings specific to the current system in your top-level folder with a settings.json
. The settings.json
probably wouldn't be committed to version control and would contain things like local db passwords and personal cloud storage keys.
This file can contain both defaults and per-environment settings.