8000 GitHub - alidavut/konfig: Config management module for Node.js. Automatic, environment specific and dynamic.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

alidavut/konfig

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KONFIG

Konfig is a config management module which allows you to load json and yaml files automatically by environment in node.js applications. You can also define dynamic values which can be used especially for dynamic environment variables on Heroku like services.

Installation

First intall module from npm :

$ npm install konfig

Create a folder named config under the root directory of the project. Then load Konfig in your application file :

var config = require('konfig')()

// Don't forget parentheses to call the function

We recommend you to define config variable as global :

global.config = require('konfig')()

You can also use different folder name except config by passing path variable while loading Konfig :

global.config = require('konfig')({ path: './another_directory' })

Usage

Let's create an example config file under config folder named app.json or app.yml

{
    "default": {
        "port": 3000,
        "cache_assets": true,
        "secret_key": "7EHDWHD9W9UW9FBFB949394BWYFG8WE78F"
    },

    "development": {
        "cache_assets": false
    },

    "test": {
        "port": 3001
    },

    "staging": {
        "port": #{process.env.PORT},
        "secret_key": "3F8RRJR30UHERGUH8UERHGIUERHG3987GH8"
    },

    "production": {
        "port": #{process.env.PORT},
        "secret_key": "3F8RRJR30UHERGUH8UERHGIUERHG3987GH8"
    }
}

If you use yaml format the config file will be like this :

default:
    port: 3000
    cache_assets: true
    secret_key: 7EHDWHD9W9UW9FBFB949394BWYFG8WE78F

development:
    cache_assets: false

test:
    port: 3001

staging:
    <<: *production

production: &production
    port: #{process.env.PORT}
    secret_key: 3F8RRJR30UHERGUH8UERHGIUERHG3987GH8

After creating config files, let's try it by using node interpreter. But first ensure that the module is installed npm install konfig and open node interpreter in the root directory of the project :

$ node

Then load the module

> var config = require('konfig')()

Let's try to get port and secret key values, remember that the default environment will be development and the config object structure will be config.[filename].[config_key]...

> config.app.port
3000
> config.app.secret_key
'7EHDWHD9W9UW9FBFB949394BWYFG8WE78F'

Quit interpreter, and let's open the interpreter with staging environment with port 4567. If you look at the config file you will see the staging port is dynamic value. Konfig enables you to use node variables in your config file by using #{} signs.

$ NODE_ENV=staging PORT=4567 node
> var config = require('konfig')()

Now, we will get some config values:

> config.app.port
4567
> config.app.secret_key
'3F8RRJR30UHERGUH8UERHGIUERHG3987GH8'

Notice that default values copies itself to the config if there is no key in the environment config with the same name.

About

Config management module for Node.js. Automatic, environment specific and dynamic.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0