Open In App

How to store deployment configuration files in Node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

Configuration files (CONFIG file ) are files used by various computer applications which consists of parameters which determine the settings required to run the application program successfully. Generally, they are used before writing any software application program.

Every website has a specific function, such as gathering information about their users. For example, it is an online shopping website then it stores the payment information of its users. Now, considering personal information gateways on a website the URL for it is stored in the config file but it varies according to the deployment. For example, if it is on the development server then the URL will be info.gfg.com and when it is dealing with production server then the URL will be secureinfo.gfg.com . Such types of settings are configured easily, if we take an example of payment gateway as well they can be managed easily but when the application has to deal with more and more deployments then it gets difficult to handle them.

Solution of the problem: The config module is a module that is used to solve this problem as it organizes configurations in a hierarchical manner for application deployments. It means that each deployment has a config file assigned to it specifically and they are accessed in a certain optimum order.

By following the steps given below we can overcome the problem:

  • Step 1: Install the config module by running the following code:
    npm i config — save
  • Step 2: Config module deals with config files in config directory at root level. You can change it to another directory by executing the following code:
    app.js.
    process.env['NODE_CONFIG_DIR'] = __dirname + '/configDir/';
    
  • Step 3: Config files are loaded into the following order:
    default.json
    {deployment}.json
    local.json
    local-{deployment}.EXT
    

    The module chooses deployment from NODE_ENV environment variable. If the value of NODE_ENV is development then development.json will be used. The same condition applies to other files as well.

  • Step 4: We have two configuration files, one for local development and one for production.
    local.json
    {
     'dbUrl': 'mongodb://localhost:27017/mydb'
    }
    production.json
    {
     'dbUrl': 'mongodb://some-db-location/dbname'
    }
    var config = require(‘config’);
    config.get(‘dbUrl’);
    

By writing the extra code [(var config = require('config'); config.get('dbUrl');] we can solve our problem. The condition mentioned in the previous step is applied here as well as, if the development is happening in a local environment, then the value for dbUrl will be taken from local.json and when it is in the production environment (NODE_ENV = production), then the value will be automatically taken from production.json. This solution solves the problem to configure multiple files according to application deployment needs.


Last Updated : 27 Feb, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads