c11n
c11n
is a dead simple configuration loader and parser for node deployments.
It outputs an object that encapsulates unified configuration settings from
environment variables, json files and command line arguments.
Usage
Let's consider we have an example.json
file with the following content:
Drop a few lines into index.js
:
const conf =
Finally, expose settings to env
and argv
as well and run our app:
export my_app_domain=my-great-app.comexport my_app_redis_auth=foobarexport my_app_cookie_secret=foobarexport my_app_cookie_maxAge=365 node ./ --cookie_secret=barfoo --http_port=8080
Output:
domain: 'my-great-app.com' redis: auth: 'foobar' host: 'redis.my-great-app.com' port: 6379 cookie: secret: 'barfoo' maxage: 365 facebook: id: 123456789 key: 'myfacebookapikey' http: port: 8080
API
var c11n =
c11n([filePaths...], [options], [callback]) ⇒ Promise
options.prefix Include only those env
variables which are starting with this prefix. Defaults to null
.
options.dir Directory path to try to find and include NODE_ENV
.json. Defaults to null
.
options.dirs An array of directory paths. Each of them will be treated like options.dir.
Gather and merge configuration from env
, additional .json
files and argv
in this order.
c11n
supports both error-first callback
and Promise
APIs.
Note If no file extension provided in a filePath
then .json
will be used.
c11n.load([filePaths...], [options], [callback]) ⇒ Promise
It's the module.exports
of this package, same as the above-mentioned.
c11n.loadFromEnv([options]) ⇒ Object
options.prefix Include only those env
variables which are starting with this prefix. Defaults to null
.
Parse configuration from process.env
and return result immediately.
c11n.loadFromFile(path, [options], [callback]) ⇒ Promise
Read and parse configuration from the file at given path
.
Please notice that, it's an async function.
c11n.loadFromArgv([options]) ⇒ Object
Parse configuration from process.argv
and return result immediately.
This function uses minimist internally for pre-parsing.
c11n.loadFromObject(object, [options]) ⇒ Object
Parse configuration from any given object
and return result immediately.
Used internally by other methods of this module.
This function iterates over keys of an object and breaks it into sub-objects along the '.'
s and '_'
s as separator characters.
Leading and trailing separators are ignored, repeating separators are reduced to a single '_'
.
Example
const obj = '__redis_port': 6379 'redis__host': 'localhost' 'domain___': 'my-domain.com' 'http.port': 8080 '_https...port': 8081 console
Output:
redis: port: 6379 host: 'localhost' domain: 'my-domain.com' http: port: 8080 https: port: 8081
If a key has a value but there are nested settings for the same key, then the original value will be accessible under the special key: '_'
.
Example
const obj = domain: 'example.com' cache: true cache_ttl: 1209600 cache_size: '300mb' console
Output:
domain: 'example.com' cache: _: true ttl: 1209600 size: '300mb'
options.freeze
If set to false
then resulting object will be mutable. Defaults to true
.
All the above-mentioned methods are accepting this option.
Installation
With npm:
npm install --save c11n
With git:
git clone git://github.com/schwarzkopfb/c11n.git
cd c11n
npm test