Configuration file module
This module can be used to load in different configuration settings based on a target environments.
In almost every project there are platform and environment specific settings. Think about paths, port numbers, databases and web services, credentials, etc, etc.
Install Crafity Config is via NPM
$ npm install crafity-config
Or by cloning this repository
$ git clone https://github.com/Crafity/crafity-config.git$ cd crafity-config$ npm install
To load the config.json data use the following JS code.
// Require the crafity config modulevar configuration = require'crafity-config';// Open the default configurationconfigurationopenif err throw err;// Work with the configuration;
pathString The JSON configuration file to load.
infoBoolean Print verbose output to the console
path defaults to
info defaults to
Opens a configuration file using the
path argument and calls the
Here is an example of loading a config file and printing the status to the console:
var configuration = require'crafity-config';configurationopen"config.json" trueif err throw err;console.logconfig;
Let's take a look at a sample configuration file for a web server.
Save this configuration to a file called config.json and place it in the root of your application. Of course another name and path can be used as well, but that needs to be specified explicitly (see The API section).
So what are these environments and how do you configure them?
In the example JSON above you can see three environments: Shared, development and production.
Only the shared environment is a special case, the other two are made up.
As the names states the shared environment shares its settings with the other custom defined environments. Often there are settings that do not differ from one environment to the next or most of the environments share the same setting except for a few. To prevent a lot of duplication the shared environment can be used. By default an environment inherits all the shared settings. The environment specific settings are then added or overwritten.
In the sample JSON configuration above you can see the following scenarios:
###Select an environment When a configuration file is loaded the environment property will be used to set the default environment. In the sample above the development environment will be used by default.
To overide the default environment a NODE_ENV variable can be specified on the command line to force a specific environment. In the example below the production environment will be used:
$ NODE_ENV=production node main.js
NB. The environment name is case sensitive.
(The MIT License)
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.