node-json-cfg
This module is designed to provide a more convenient shared configuration system.
Overview
When coding in nodejs environment, you may encounter conditions in which you need to keep track all configurations among submodules. To fulfill the requirement, you may come up with a customized module. But typically, even the main module and the submodules are dependent to the same customized module, you'll end up with modules with different instances. This is very frustrating...
No worry, this module is also designed to solve the above situation. As long as you include the module with the same version, you'll have the ability to share configuration storage among all your modules and submodules.
Feature
- An easy to use configuration holder
- Easy to load / access
- Synchronize among all modules including ones required by submodules
Installation
All in have to do is...
$ npm install json-cfg
Require the module within your scripts and use it!
let config = ;
Examples
General Usages
All configurations can be accessed via conf
property of the module.
For example, you have two configuration files config1.json and config2.json
// config1.json "https": false "address": "localhost" "port": 8080
// config2.json "https": true "address": "www.google.com" "port": 443
You can load them and write in local moduleA...
// moduleA.js // You can load multiple configurations and this module will merge them into the same spacelet config = ; // writing configurationconfigconfport = "80";
and you can access the configurations in local moduleB...
// moduleB.js let config = ;let https:isHttps address port = configconf; const http = ; // ... do http initialization logics here...http;
Interestingly, you can also access the same configurations from dependent external module external.js
let config = ; moduleexports = { console; // www.google.com console; // 80 console; // https };
Accessing the Shared Space
You can get the shared configuration space using following line
let shared = configtrunk;
Separated Spaces
In some conditions, you may want to store different configurations separately.
This can be done via following usage...
let otherSpace = config;
You can always obtain the same configuration space by the same space identifier.
Moreover, if you're done with specific space, you can remove it via following line...
config;
Reseting
If you want to clear a space, just do the following lines...
config;config;
Note that the reset
method only replace the internal configuration holder with newer object. This method won't affect the target object itself.