var conf = { // - // "production", "development", "test env: process.env.NODE_ENV || "development", // IP ip: process.env.IP_ADDRESS || "127.0.0.1", // port: process.env.PORT || 0, // database: { host: process.env.DB_HOST || "localhost:8091" } }; module.exports = conf;
var conf = convict({ env: { doc: "The applicaton environment.", format: ["production", "development", "test"], default: "development", env: "NODE_ENV" }, ip: { doc: "The IP address to bind.", format: "ipaddress", default: "127.0.0.1", env: "IP_ADDRESS" }, port: { doc: "The port to bind.", format: "port", default: 0, env: "PORT" }, database: { host: { default: "localhost:8091", env: "DB_HOST" } } }); conf.validate(); module.exports = conf;
format
property indicates either one of the built-in convict types ( ipaddress
, port
, int
, etc.) or a function for validating user types. If the parameter fails type checking during validation, an error occurs.env,
set, then its value will be used instead of the default value.doc
property is quite obvious. The advantage of including the documentation in the schema before comments in the code is that this information is used in the conf.toSchemaString()
method for more informative output.conf.load()
and conf.loadFile()
calls. For example, you can load additional parameters from a JavaScript object for a specific environment: var conf = convict({ // , }); if (conf.get('env') === 'production') { // conf.load({ port: 8080, database: { host: "ec2-117-21-174-242.compute-1.amazonaws.com:8091" } }); } conf.validate(); module.exports = conf;
conf.loadFile()
: conf.loadFile('./config/' + conf.get('env') + '.json');
loadFile()
can also load several files at once if you pass an array of arguments: // CONFIG_FILES=/path/to/production.json,/path/to/secrets.json,/path/to/sitespecific.json conf.loadFile(process.env.CONFIG_FILES.split(','));
load()
and loadFile()
is useful when there are settings for each of the environments that should not be set in the environment variables. Separate declarative configuration files in JSON format allow you to more clearly present the differences between the parameters in different environments. And since files are uploaded using cjson , they can contain comments, which makes them even more understandable.load()
and loadFile()
. To check which settings are valid, you can call conf.toString()
.url
, ports
or ipaddress
, and you can also use built-in JavaScript constructors (for example, Number
). If the format
property is not specified, convict will check the type of the parameter to match the default value type (by calling Object.prototype.toString.call ). The following three schemes are equivalent: var conf1 = convict({ name: { format: String default: 'Brendan' } }); // , , // , var conf2 = convict({ name: { default: 'Brendan' } }); // var conf3 = convict({ name: 'Brendan' });
["production", "development", "test"]
. Any value that is not in the list will not pass validation. var check = require('validator').check; var conf = convict({ key: { doc: "API key", format: function (val) { check(val, 'should be a 64 character hex key').regex(/^[a-fA-F0-9]{64}$/); }, default: '3cec609c9bc601c047af917a544645c50caf8cd606806b4e0a23312441014deb' } });
conf.validate()
return detailed information about each erroneous setting, if any. This helps to avoid redeploying the application when each configuration error is detected. Here's what the error message will look like if we try to set the key
parameter from the previous example to the value 'foo'
: conf.set('key', 'foo'); conf.validate(); // Error: key: should be a 64 character hex key: value was "foo"
Source: https://habr.com/ru/post/197166/
All Articles