ocbesbn-config
This module provides easy access to a consul service registry. It provides automatic key prefixing for key storage isolation and caching for keys and endpoints. For further details, please have a look at the wiki.
Minimum setup
First got to your local code directory and run:
npm install ocbesbn-config
To go with the minimum setup, you need to have a consul instance up and running.
Now go to your code file and put in the minimum setup code.
const config = require('ocbesbn-config');
config.init({}).then(() => config.getProperty('my-key')).then(console.log).catch(console.log);
If everything worked as expected, you will get a bluebird promise as result.
Request multiple keys
In addition to single-key-requests, the config module provides two different methods for multi-key-requests.
- Key-prefixing (like paths).
- List of keys.
Keys by prefix
Prefixing allows you to recursively fetch consul keys and their corresponding values by simply passing a prefix and the recursive parameter to the getProperty() method of the config module. After all keys have been retrieved successfully, the resulting promise contains an object in which the (full) consul keys represent the actual keys and their values represent the actual values inside consul.
config.init({}).then(() => config.setProperty("path/to/key1", "value1"))
.then(() => config.setProperty("path/to/key2", "value2"))
.then(() => config.setProperty("path/to/key3", "value3"))
.then(() => config.getProperty("path/", true))
.then(console.log)
.catch(console.log);
Keys by list
The config module allows passing an array of keys or prefixes (but not mixed!) to the getProperty() method. After all keys have been retrieved successfully, the resulting promise contains an array of consul values each at the corresponding position of their input keys.
config.init({}).then(() => config.setProperty("key1", "value1"))
.then(() => config.setProperty("key2", "value2"))
.then(() => config.setProperty("key3", "value3"))
.then(() => config.getProperty([ "key1", "key2", "key3" ]))
.then(console.log)
.catch(console.log);
Default configuration
The default configuration object provides hints about what the module's standard behavior is like. It is mostly recommended to leave most settings as they are and treat them more as general conventions to a common structure
in order to maintain a common setup across different services. For further details, please have a look at the wiki.
{
host: 'consul',
port: 8500,
retryCount : 5,
retryTimeout : 1000
}