Skip to content

Manage the configuration of your Node.js app

License

Notifications You must be signed in to change notification settings

WHASQ/rduk-configuration

 
 

Repository files navigation

RDUK - configuration

Manage your Node.js app configuration

Build Status Coverage Status bitHound Overall Score

Installation

npm install @rduk/configuration --save --save-exact

Naming

Add the configuration file at the root of your project. You have to name it according to the argument passed to the factory.

var configuration = require('@rduk/configuration');

var myConfig;

/* config.yml */
myConfig = configuration();

/* config.dev.yml */
myConfig = configuration('dev');

/* config.prod.yml */
myConfig = configuration('prod');

Note:

If you set NODE_ENV, you can access the configuration according to it

myConfig = configuration.load(); //will load config.dev.yml if NODE_ENV === dev

Note:

If you prefer put your config file within another folder, you can do so by adding in the package.json of your solution a rduk configuration section.

# example of package.json (path/to/config/app.yaml)
{
    ...
    "rduk": {
        "config": {
            "path": "path/to/config", # (default: PWD)
            "ext": ".yaml", # (default: .yml)
            "prefix": "app" # (default: config)
        }
    }
}

or use RDUK_CONFIG_* environment variables as follow :

  • RDUK_CONFIG_PATH=path/to/config
  • RDUK_CONFIG_EXT=.yaml
  • RDUK_CONFIG_PREFIX=app

Reference

Configuration

Configuration.settings

Get the SettingsSection of the config file

yaml:

settings:
    facebook:
        appId: APP_ID
        appSecret: APP_SECRET

usage:

var fbSettings = myConfig.settings.get('facebook');

console.log(fbSettings.appId); //will output APP_ID
console.log(fbSettings.appSecret); //will output APP_SECRET

Configuration.connections

Get the ConnectionsSection of the config file

yaml:

connections:
    -
        name: con1
        host: localhost
        user: db1_user
        password: 123456
        database: db1
    -
        name: con2
        host: localhost
        user: db2_user
        password: 123456
        database: db2
    -
        name: mapbox
        token: my_mapbox_token
    -
        name: gmap
        token: my_gmap_token

usage:

var con1 = myConfig.connections.get('con1');
var con2 = myConfig.connections.get('con2');

console.log(myConfig.connections.get() === myConfig.connections.get('con1')); //will output true

Configuration.getSection(name, type)

Get the specified section

yaml:

map
    default: mapbox
    providers:
        -
            name: mapbox
            type: MapBoxProvider
            connection: mapbox
        -
            name: gmap
            type: GoogleMapProvider
            connection: gmap

section:

var MapSection = function(section) {
    /* ... */
};

usage:

var map = myConfig.getSection('map', MapSection);

Note:

If no type passed to the method, get the raw section.


License and copyright

See LICENSE

About

Manage the configuration of your Node.js app

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%