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.
confit
adds support for adding JavaScript-style comments in your json files as each file is processed by shush before being merged into your config.
Usage
var 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.- returns - config factory.
'use strict'; var path = ;var confit = ; var basedir = path;;
config factory
addOverride(filepath)
(or)addOverride(obj)
- Use this to add file (.json or .js), to merge with the config datastore and override the overlapping data if any. Alternatively, you can also pass a json object to override.addDefault(filepath)
(or)addDefault(obj)
- Use this to add default file (.json or .js), to merge with the config datastore and serve as the default datastore. Alternatively, you can also pass a json object for defaults.create(callback)
- Creates the config object, ready for use. Callback signature:function (err, config) {}
// All methods besides `create` are chainable //or .addDefault({foo: 'bar'}) //or .addOverride({foo: 'baz'}) ; // - or -//// var factory = confit(options);// factory.addOverride('./mysettings.json');// factory.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 either handler implementations or an array or handler implementations. These protocols will be used to process the config data prior to registration. If using an array of handler implementations, each handler is run in series (seeMultiple handlers
in the shortstop README).defaults
(String) - the name of the file containing all default values. Defaults toconfig.json
.envignore
(Array) - any properties found inprocess.env
that should be ignored
'use strict'; var path = ;var confit = ;var handlers = ; var options = basedir: path protocols: file: handlers glob: handlers ; ;
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;config; // 'bar' config;config; // 'baz' config;config; // 'd'
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 so values starting with prod
become
production
, starting with stag
become staging
, starting with test
become test
and starting with dev
become development
.
// NODE_ENV='dev'config; // 'dev'config; // 'development'config; // trueconfig; // falseconfig; // falseconfig; // false
// NODE_ENV='custom'config; // 'custom'config; // 'custom'config; // falseconfig; // falseconfig; // falseconfig; // falseconfig; // true
Precedence
Precedence takes the following form (lower numbers overwrite higher numbers):
- command line arguments
- env variables
- environment-specific config (e.g.,
development.json
) - main config (
config.json
) env
normalization (env
,env:development
, etc)
Shortstop Handlers
Confit by default comes with 2 shortstop handlers enabled.
import:
Merges the contents of the specified file into configuration under a given key.
config:
Replaces with the value at a given key. Note that the keys in this case are dot (.) delimited.