0.0.2 • Published 4 years ago

@ashwindmk/configer v0.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

Configer

Node JS library to easily set application environment and use configuration constants.

npm version

Install

npm i @ashwindmk/configer

Create Config JSON File

Sample config.json

{
    "development": {
        "mysql": {
            "host": "http://localhost:5000/mydb",
            "port": 3306,
            "user": "local",
            "password": "pass1234"
        }
    },
    "staging": {
        "mysql": {
            "host": "https://staging.mydb.com:3000/mydb",
            "port": 3306,
            "user": "admin",
            "password": "staging1234"
        }
    },
    "production": {
        "mysql": {
            "host": "https://prod.mydb.com:3000/mydb",
            "port": 3306,
            "user": "admin",
            "password": "secret1234"
        }
    }
}

Here development and production are the two different environments.

Set the config JSON File

Make sure you set the absolute path to the config JSON file.

const configer = require('@ashwindmk/configer');
const path = require('path');

const absPath = path.resolve('./config.json');
configer.setFile(absPath);

Set the Environment

configer.setEnv('development');
For setting environment in Node JS Applications:

package.json

{
    ...
    "scripts": {
        "start": "node index.js production",
        "start-stag": "node index.js staging",
        "start-dev": "nodemon index.js development",
        ...
    }
}

index.js

const configer = require('@ashwindmk/configer');

const env = process.argv[2];
configer.setEnv(env);

Usage

let dbHost = configer.get('mysql', 'host');
let dbPort = configer.get('mysql', 'port');