HOT-CONFIG
A node.js config loader is designed to be able to hot-reload.
FEATURES
- Load recursively config files that matches a given pattern.
- Built-in asynchronous YAML and JSON parsers.
- Support environment-specific (profile-specific) configs.
- Hot-reloadable.
USAGE
# ./configs/generic.yaml # ------------------------------------------------------------------------------ default: debug: trueproduction: debug: false
# ./configs/db/mongo.yaml # ------------------------------------------------------------------------------ default: host: 'localhost' port: 27017production: host: '12.12.12.12'
# ./configs/movie-catalog.yaml # ------------------------------------------------------------------------------ default: cache: 0production: cache: -1
// ./index.js// ----------------------------------------------------------------------------- const config = ;const app = ; config;
// ./src/app.js// -----------------------------------------------------------------------------const config = store; { if !processenvNODE_ENV to; to; to; to; if processenvNODE_ENV === 'production' to; to; to; to; } moduleexports = start;
APIs
config.store
The config store.
config.filePatterns
Built-in file patterns, yaml
and json
.
config.fileParsers
Built-in file parsers, yaml
and json
.
config.ParserError
A Error-based class provides information of a parser error.
config.load(dir, opts, done)
Load config files recursively in a directory.
/** * Load configs in a directory. * @param * @param * @param * @param * @param * @param * @param * @param * @param */ {}
config.clear()
Clear the config store.
HOT-RELOADABLE
The key of hot-reloading is the only config store instance.
Take a look at:
After all config files are parsed and merged, the config store will be cleared and updated synchronously but not replaced by a new instance, this makes all existing references of the config store are still working after reloaded.
But keep in mind that this feature only works if you're keeping references of the store, not with references of config values. For example:
const config = store; // this is hot-reloadable!; const mongoConfig = storemongo; // this is not hot-reloadable!;
PREFER JSON
If you prefer JSON, you need to do some extra works:
const config = ; const opts = filePattern: configfilePatternsjson fileParser: configfileParsersjson config
DRY RUN
To load configs for testing, you can use the loading opts dryRun
.
const config = ; const opts = dryRun: true config