confit
Simple, environment-based configuration. confit
loads a default JSON
configuration file, additionally loading environment-specific files, if applicable.
It will also process the loaded files using any configured
shortstop protocol handlers.
(See Options below.)
Usage
var confit = require('confit');
confit([options])
options
(String | Object) - the base directory in which config files live or a configuration object. If no
arguments is provided, defaults to the directory of the calling file. Signature function (err, config) {}
- returns - config factory.
'use strict';
var path = require('path');
var confit = require('confit');
var basedir = path.join(__dirname, 'config');
confit(basedir).create(function (err, config) {
config.get;
config.set;
config.use;
config.get('env:env');
});
config factory
addOverride(filepath)
- Register a file (JSON or JS), the contents of which should be merged with the config datastore.create(callback)
- Creates the config object, ready for use. Callback signature: function (err, config) {}
confit(options)
.addOverride('./mysettings.json')
.addOverride('./mysettings.json')
.create(function (err, config) {
});
Options
basedir
(String) - the base directory in which config files can be found.protocols
(Object) - An object containing a mapping of
shortstop protocols to handler implementations.
This protocols will be used to process the config data prior to registration.defaults
(String) - the name of the file containing all default values.
Defaults to config.json
.
'use strict';
var path = require('path');
var confit = require('confit');
var handlers = require('shortstop-handlers');
var options = {
basedir: path.join(__dirname, 'config');
protocols: {
file: handlers.file,
glob: handlers.glob
}
};
confit(options).create(function (err, config) {
});
Config API
get(key)
- Retrieve the value for a given key. Colon-delimited keys can be used to traverse the object hierarchy.set(key, value)
- Set a value for the given key. Colon-delimited keys can be used to traverse the object hierarchy.use(obj)
- merge provided object into config.
config.set('foo', 'bar');
config.get('foo');
config.use({ foo: 'baz' });
config.get('foo');
config.use({ a: { b: { c: 'd' } } } );
config.get('a:b:c');
Default Behavior
By default, confit
loads process.env
and argv
values upon initialization. Additionally,
it creates convenience environment properties prefixed with env:
based on the
current NODE_ENV
setting, defaulting to development
. It also normalizes
NODE_ENV
settings to the long form, so dev
becomes development
, prod
becomes production
, etc.
config.get('NODE_ENV');
config.get('env:env');
config.get('env:development');
config.get('env:test');
config.get('env:staging');
config.get('env:production');
config.get('NODE_ENV');
config.get('env:env');
config.get('env:development');
config.get('env:test');
config.get('env:staging');
config.get('env:production');
config.get('env:custom');