Config module
The config module allow to access a hierarchical source of properties. Those source of those properties can be files, environment variables or application arguments. The default implementation is based on nconf
Formats
The supported file formats are json
and json5
.
Default configuration sources
The source of the properties is given by the following precedence (top overwrites):
- Environment variables, being
_
the hierarchical separator - Application arguments
- A file from the environment variable
LOCAL_CONFIG_FILE
- A loop of the
configPaths
passed to MicroBase during the instantiation, including MicroBase itself:- A file from the application folder:
${path}/config/${process.env.NODE_ENV || 'development'}.json
- A file from the application folder:
${path}/config/defaults.json
- A file from the application folder:
- A file from MicroBase:
config/defaults.json
Please take a look at the examples to see how to instantiate MicroBase.
Usage
For a file like this one:
{
"mykey": {
"mysubkey": 2
}
}
The following lines will output 2
console.log(base.config.get('mykey:mysubkey'));
To set a property use the set
method:
base.config.set('mykey:mysubkey', 3);
Custom configuration
To change add configuration paths, pass them in the config
object at microbase startup:
const MicroBase = require('microbase');
const base = new MicroBase({
configPaths: [{myAppPath: __dirname}]
});