This module helps to easily manage all the configuration properties needed in a system, giving a simple and consistent configuration interface to your application. The properties must have a profile category, production,dev or test. File properties are stored in folder config in a file named properties.json within your application home directory(.../config/properties.json). The package use npm minimist, so your properties can be overridden by command line parameters. The package is compliant to JSON5 so you can add single and multi line comments like // or /% %/ in your JSON file
- Installation
- Property file creation
- Property file population
- Using propertiesmanager
- Using propertiesmanager in development npm package
- Loading a running profile
- Ovverride parameters from command line
- Examples
To install propertiesmanager, type:
$ npm install propertiesmanager
Configuration files must be created in a folder named config
in the home directory of your application.
The filename must be named default.json
. Type:
$ mkdir config
$ vi config/default.json
The file containing your properties is a JSON file having a mandatory dictionary called "production". It contains all the configuration parameters of your application running in production or default mode. Other dictionaries can be used, "dev" for development properties and "test" for testing properties.
An example of empty property file:
{
"production":{}
}
An example of populated property file:
{
"production":{
"properties_One":"One",
"properties_Two":"Two",
"Objectproperties":{
"Obj_One":1,
"Obj_Two":2
}
}
}
An example of property file with dev and test dictionaries defined:
{
"production":{
"properties_One":"One",
"properties_Two":"Two",
"Objectproperties":{
"Obj_One":1,
"Obj_Two":2
}
},
"test":{
"properties_One":"TestOne",
"Objectproperties":{
"Obj_One":1,
"Obj_Two":2
}
},
"dev":{
"properties_One":"Test Development",
"DevLogs":{
"path":"/logs/log.xml",
"format":"xml"
}
}
}
Just require it like a simple package:
var propertiesmanager = require('propertiesmanager').conf;
propertiesmanager returns a dictionary containing all the properties from a configuration file. These properties can be overridden by command line parameters.
// print the loaded properties dictionary
console.log(propertiesmanager);
If you use this package to develop other node_modules, then add "install": "npm install propertiesmanager"
in the
scripts
tag in your package.json as below:
// yur package.json
{
......
......
"scripts": {
.......
"install": "npm install propertiesmanager"
},
......
......
}
You need to do this because propertiesmanager looks for the property file (config/default.json) in a folder located two levels up the node_modules folder, so propertiesmanager must be installed in the node_modules folder of the package that uses it as a dependence
The application using this package runs under one profile among three (production, dev, test), set by NODE_ENV environment variable. If NODE_ENV is not defined the default profile is production
Running your app in default mode. production properties are loaded:
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ npm start
Running your app in production mode. production properties are loaded:
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=production npm start
Running your app in dev mode. dev properties are loaded:
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev npm start
Running your app in test mode. test properties are loaded:
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=test npm start
The package propertiesmanager use npm minimist, so your properties stored in default.json can be
overridden by command line parameters. Just type in command line --ParamName=ParamValue
, as in:
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev npm start -- --properties_One="Override_TestOne"
The first --
after npm start
means that the following params must be passed to node bin/www
,
so if you run your application directly calling node bin/www
the first --
must be not used, as in:
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev node bin/www --properties_One="Override_TestOne"
To override parameters that are complex objects, use dotted (".") notation. For example:
// We want to override Obj_One
{
"production":{
"properties_One":"One",
"properties_Two":"Two",
"Objectproperties":{
"Obj_One":1,
"Obj_Two":2
}
}
}
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev node bin/www --Objectproperties.Obj_One="Override_Obj_One"
For further information about passing parameter see https://www.npmjs.com/package/minimist
From your home project directory type:
$ mkdir config
$ vi config/default.json
Write default.json
property file:
{
"production":{
"properties_One":"One",
"properties_Two":"Two",
"Objectproperties":{
"Obj_One":1,
"Obj_Two":2
}
},
"test":{
"properties_One":"TestOne",
"Objectproperties":{
"Obj_One":1,
"Obj_Two":2
}
},
"dev":{
"properties_One":"Test Development",
"DevLogs":{
"path":"/logs/log.xml",
"format":"xml"
}
}
}
Now you can print all your properties:
var propertiesmanager = require('propertiesmanager').conf;
// print the loaded properties dictionary
console.log("########### Readed Properties ###########" );
console.log(propertiesmanager);
Running your app in default mode, production properties are loaded:
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ npm start
########### Read Properties ###########
"production":{
"properties_One":"One",
"properties_Two":"Two",
"Objectproperties":{
"Obj_One":1,
"Obj_Two":2
}
}
Running your app in production mode (NODE_ENV=production)
is equivalent to run in default mode:
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=production npm start
########### Readed Properties ###########
"production":{
"properties_One":"One",
"properties_Two":"Two",
"Objectproperties":{
"Obj_One":1,
"Obj_Two":2
}
}
Running your app in dev mode (NODE_ENV=dev)
, dev properties are loaded:
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev npm start
########### Readed Properties ###########
"dev":{
"properties_One":"Test Development",
"DevLogs":{
"path":"/logs/log.xml",
"format":"xml
}
}
Running your app in test mode (NODE_ENV=test)
, test properties are loaded:
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=test npm start
########### Readed Properties ###########
"test":{
"properties_One":"TestOne",
"Objectproperties":{
"Obj_One":1,
"Obj_Two":2
}
}
Overriding some test mode (NODE_ENV=test)
properties:
$ cd "YOUR_APPLICATION_HOME_DIRECTORY"
$ NODE_ENV=dev npm start -- --properties_One="Override_TestOne"
########### Readed Properties ###########
"test":{
"properties_One":"Override_TestOne",
"Objectproperties":{
"Obj_One":1,
"Obj_Two":2
}
}
MIT License
Copyright (c) 2016 aromanino
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
CRS4 Microservice Core Team (cmc.smartenv@crs4.it)
Alessandro Romanino (a.romanino@gmail.com)
Guido Porruvecchio (guido.porruvecchio@gmail.com)