1.0.2 • Published 7 years ago
json4config v1.0.2
json4config
A simple and minimalist environment-aware configuration provider
Installation
Run npm i json4config and start using it :-)
Introduction
Use the following code to conveniently get config values for a environment (in this case prod - see end of first line):
const cfg = require('json4config')('./config.json', 'prod');
console.log(cfg.get('mailing.host'));
console.log(cfg.get('mailing.port', 25)); // <-- Default fallback value
console.log(cfg.config); // <-- Raw config objectfrom a central config.json file with the following contents:
{
"dev": {
"mailing": {
"host": "localhost"
},
"port": 10001
},
"prod": {
"extends": "dev",
"mailing": {
"host": "smtp.gmail.com"
},
"port": 80
},
"app.mydomain.tld": {
"extends": "prod",
"mailing": {
"host": "custom-smtp.myserver.foo",
"port": 465,
"useSSL": true
}
}
}Features
- Put different config values in a single
config.jsonfor a NodeJS application - Use the keyword
extendsinside an environment configuration to get all config keys from the provided environment. This way overwriting just only some values is very easy - It is easy to place config values for different target systems of the same application in one place
- Provide either as a second parameter the name of the environment or add the commandline switchs
-e prodor--env prodor place it inside theNODE_ENVsystem environment variable - Access can be gained either by using the function
get(key, defaultValue)with a default value or by using theconfigkey which contains the entire object - See the example in
examples/ Uses tests to ensure working code
ToDo's
- Tests need to be extended, to also test the commandline switches and
NODE_ENVsystem variable This documentation needs to be extended with the optional parameters and the normalization feature of environment names