Manage your Node.js app configuration
npm install @rduk/configuration --save --save-exact
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
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
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
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.
See LICENSE