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):

  1. Environment variables, being _ the hierarchical separator
  2. Application arguments
  3. A file from the environment variable LOCAL_CONFIG_FILE
  4. A loop of the configPaths passed to MicroBase during the instantiation, including MicroBase itself:
    1. A file from the application folder: ${path}/config/${process.env.NODE_ENV || 'development'}.json
    2. A file from the application folder:${path}/config/defaults.json
  5. 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}]
});