nb-config
Simple server-side file-based configuration manager
Main Feature
- Easy to Use
- Separated default configuration for docker deployment(data volume)
- Running Target
- Caching: You can use the config as an instance as well as a singleton.
Planned Feature
- Support various file extension
Usage
Installation
npm install nb-config --save
Basic Usage
DEFAULT FILE: ./config/default.js
value1: hello
value2: world
CONFIG FILE: ./config/development.js
value1: hi
./your.js
const Config = require('nb-config');
let config = new Config();
console.log(config.get('value1'), config.get('value2'));
output
# node your.js
hi world
Working with some parameters
example project directory
|/
|--src/
|----your.js
|--config/
|----myProject.default.yaml
|----myProject.development.yaml
|----myProject.production.yaml
load configuration with specific moduleName(default: name field on package.json) and runningTarget(default is 'development')
const Config = require('nb-config');
let config = new Config('myProject', 'production');
// config will contains data in myProject.default.js + myProject.production.yaml
runningTarget could be replace with environment NODE_ENV
const Config = require('nb-config');
let config = new Config('myProject');
// test with NODE_ENV=production node ./src/your.js
// will returns same value with above example
For docker deployment
example project directory
|/
|--src/
|----your.js
|--config/
|----myProject.development.yaml
|----myProject.production.yaml
|--myProject.default.yaml
docker dataVolume : /host/myProject/config:/container/config
|config/
|--myProject.development.yaml
|--myProject.production.yaml
Once the data volume is mounted in the container, all of the files in the container will be erased.
The problem is even default file also will be erased.
In this case nb-config will useful.
const Config = require('nb-config');
let config = new Config('myProject', null, {
defaultDir: process.cwd()
});
After data-volume mounted, nb-config will copy ./myProject.default.yaml
into empty /config
directory
Now, docker host can see full schemed default file in data-volume
Specification
Environment Variables
- NODE_ENV: runningTarget, see NBCofig
- NB_CONFIG_DIR: directory where configurations stored
- NB_DEFATUL_DIR: directory where default file stored
class NBConfig
constructor(['moduleName'], ['runningTarget'], [{options}])
initialize and load configurations
(optional) moduleName
If this field specified, NBConfig will use this moduleName as prefix of configuration files
// it will load default.yaml + development.yaml
let config1 = new NBConfig();
// it will load myProject.default.yaml + myProject.development.yaml
let config2 = new NBConfig('myProject');
(optional) runningTarget
Order of reading runningTarget
- parameter on constructor
- environment variable: NODE_ENV (process.env.NODE_ENV)
- default is 'development'
Once the value is found, the rest is passed.
(optional) options
- cache: (boolean) mark use cache or not (default is true). If config loaded from cache
config.fromCache
markedtrue
- env: (boolean) let environment variables overwrite file configuration if exist. (default is true)
- User can assign not only single config (e.g. "port") but also nested configuration path (e.g. database.url)
- Environment variables will be cast to the same type as the value specified in the configuration file.
-
__
is a default delimiter of configuration path (e.g. pathdatabase.url
-->EXPORT database__url=URL_VALUE
)
- User can assign not only single config (e.g. "port") but also nested configuration path (e.g. database.url)
- configDir: (string) directory where configurations stored
- Loading Order:
options.configDir
> process.env.NB_CONFIG_DIR
>process.cwd()/config
(as default)
- Loading Order:
- defaultDir: (string) directory where *.default.ext file stored
- Loading Order:
options.defualtDir
> process.env.NB_DEFAULT_DIR
>same value as configDir
(as default)
- Loading Order:
License
Copyright 2016 Noizbuster <noizbuster@noizbuster.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at