How to store deployment configuration files in Node.js ?
Last Updated :
27 Feb, 2020
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.
Similar Reads
How to Deploy Node.js Express Application on Render ? Deploying a Node.js Express application on Render is straightforward and involves a few key steps to set up your project, configure deployment settings, and manage your application on the Render platform. Render provides an easy-to-use platform for deploying applications, offering features like auto
4 min read
How to Create a Dockerfile in Node.js ? A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. It is something like a shellscript that gathers multiple commands into a single document to fulfill a single task. Prerequisite: Before you can Dockerize any application, you ne
4 min read
How to Deploy Node.js Application in Kubernetes? Deploying the Node.js application on Kubernetes in a containerized manner makes it highly scalable,fault-tolerant, and allows for zero downtime. Kubernetes allows container orchestration that can be used in many ways during deployment. Load balancing is provided, which helps with automatic load tran
4 min read
How to Structure my Application in Express.js ? A clean and well-organized folder structure is crucial for building maintainable and scalable Express.js applications. This article explores best practices for structuring your application, organizing components, and maintaining a modular architecture.Why Structure an Express Application?A well-stru
6 min read
How To Deploy Node Js Application In AWS Lightsail ? Lightsail can be defined as a simple, easy-to-use, and user-friendly service offered by Amazon Web Services (AWS). The main goal of Lightsail is to provide an easy way for individuals, startups, and small businesses to launch and manage virtual private servers (VPS) and other cloud services without
6 min read