Initializes the class, determining the environment, then loading the configuration for the environment and storing it in the object.
The config
parameter can be an object with the configuration values,
or a string representing a JSON/YAML/Hjson file to load, another instance of
SMConfig, or an array mixing those.
If multiple objects/files are passed, values are merged from left to right,
so subsequent values overwrite property assignments of previous values.
File type is determined by the extension: json, yml, yaml, hjson.
When using YAML files, you can also represent additional JavaScript typtes that are not allowed by JSON and Hjson:
!!js/regexp /pattern/gim
!!js/function 'function () {...}'
!!js/undefined ''
The configuration object should have the following structure:
{
// Default configuration, for all environments
default: {
key1: 'value1',
key2: 'value2'
},
// Each subsequent key is the name of the environment;
// this can be anything you want
dev: {
// Custom environments inherit all keys from the default
// environment, but can be overwritten here
key1: 'override',
newkey: 'helloworld'
},
production: {
key1: 'override'
},
otherenvironment: {},
// The hostnames object contains a list of hostnames that are
// mapped to a specific environment.
hostnames: {
// Name of the environment, then list of hostnames
dev: [
'laptop.localdomain'
],
production: [
// Can use * as wildcard
'*.example.com',
// Can also use RegExp objects
/(.*?)\.example\.com$/i
]
}
}
Configuration values can always be overridden at runtime by passing
environmental variables to the application. All values can be specified
in a string with key-value pairs, in an environmental variable called
SMCONFIG
(name can be changed with the options.envVarName
setting). You can also append _1
, _2
, etc, to pass multiple
environmental variables. Multiple key-value pairs can be passed in the
same variable, separated by a space. If the value contains a space,
quotes can be used to escape it.
It's possible to load environmental variables from a ".env" file by
specifying the file path in the SMCONFIG_FILE
(or options.envVarName + '_FILE'
) variable. This can be used with
Docker secrets too, when the path is in /run/secrets
.
Values passed via environmental variables are strings, but numeric ones
(those representing a number) are converted to numbers.
Values in the hostnames array can be RegExp objects or strings. Strings are
parsed using SMHelper.strIs
, so the *
token can be used as wildcard.
The environment is determined by, in order:
env
parameterNODE_ENV
environmental variabledefault
environmentConfiguration params or filename(s) to load
Force a specific environment
Dictionary with options
Contains the configuration for the current environment
Holds the name of the environment, as determined during initialization
All configuration parameters (read-only)
Environment name
Return value for key from configuration
Configuration key to retrieve
Value for the key
Generated using TypeDoc
Environment and configuration utilities