config-setttings
install
npm install config-settings --save
Usage
Use with JsonConfig
{
"var":"xxxx",
"integer":111,
"dir":"/var/xxx",
"dirNotExist":"1234567890qwerty",
"file":"/var/filename",
"fileDirExist":"/var/filenotexist",
"fileNotExist":"098987655oiouyuyt",
"int":11,
"notInt":"xx",
"url":"http://baidu.com"
}
const config = require('../config.json');
const {JsonConfig} = require('config-settings');
const jsonConfig = new JsonConfig(config);
var varstr = jsonConfig.loadNecessaryVar('var');//get xxxx
var integer = jsonConfig.loadNecessaryVar('integer');//get 111
Use with ConsulConfig
const {ConsulConfig} = require('config-settings');
const settings = new ConsulConfig({consulAddr:'127.0.0.1:8500'});
settings.loadNecessaryVar('var').then(function(value) {
const varstr = value;
});
settings.loadNecessaryVar('integer').then(function(value) {
const integer = value;
});
settings.allLoaded().then(function() {
//all loading progress finished
}).catch(function() {
});
Use the ConsulSyncConfig
const {ConsulSyncConfig} = require('config-settings');
const settings = new ConsulConfig({consulAddr:'127.0.0.1:8500', keys: ['var', 'integer']});
const varstr = settings.loadNecessaryVar('var');
const integer = settings.loadNecessaryVar('integer');
Attention
When call the function start with loadNecessary
failed, the module will throw Error in asynchronous way.
API
See the document of api
Test
You should set the environment variable of CONSUL_SERVER
in format of ip:port
before npm run
.