As we know, During application development and deployment we will be using different stages like dev, test, QA, pre-prod and finally production. These stages will use same services like databases, message services, authentication services ……. with different access credentials specified for each stage. If we don’t have proper profiles for each stage of the application, we might end up using the wrong keys in wrong stage otherwise spending lot of time in modifying and cross checking the keys and configurations during the deployment. So profile management is an important aspect in the application. So lets see how do we manage profiles in Node.js applications.
I will explain a sample application in which i will manage database credentials for different stages. I will be using a Node.js module called
1. Create a nodejs application:-
- Create a folder and go to the folder and run
npm initand enter all the values it asks.
- Install config module by running the command
npm install -S config.
- Basically config module requires to keep all profile data in a common folder. By default it reads from a folder called
configfrom your application root directory. You can override and specify your own folder by specifying an environment variable called
NODE_CONFIG_DIRECTORY. But i would like to keep it simple so i will create a directory called
configin my application directory.
2. specify your configurations in profile files:-
- Once you create
configfolder you can specify your profile information for each stage in files.
configwill support multiple file formats like
.json, .json5, .ts, .yaml, .coffee, .properties, .cson, .tomletc. I use .json file.
configrequires to pass a environment variable like
NODE_ENVwhile running your Node.js app. If you forgot to set NODE_ENV while running the app,
configreads the configurations from the
- Specify a
default.jsonin config directory. After default.json is provided, add different stage configurations in files like
dev.json, test.json, qa.json, pre-prod.json, prod.jsonas per your stages.
- If you have multiple instances in each stage you can even specify the config files like
- Please find the sample profile file as shown below.
3. Read stage specific data in application:-
- Once you complete adding profile files in config directory, you can read the profile specific data in the entire application as you required.
- To read the configuration from a stage specific file, import
config. After importing it’s as simple as reading a JSON.
- Please see the sample snippet as shown below.
4. Run Node.js app for specific profile:-
- Once you are done using profile specific data as you required in business logic, try running the Node.js app.
- To run the code for a specific stage, first you need to export a
NODE_ENVvariable. This name should be same as the profile file name but without file extension.
- This means you are asking
configmodule to read the keys from
- If you have any log statements you can verify the values read by config module in your business logic.
- If you have multiple instances in a stage to deploy then you can point by setting
export NODE_APP_INSTANCE=1 //by running above commands, you are asking config module to read profile information from dev.json under config folder. Once rev.json is read successfully then it will try to read from dev-1.json.
5. Override the config key while running:-
For example while running you want to override a key specified in stage specific profile file. To do that you can override by exporting a variable called
NODE_CONFIGas shown below, before running the Node.js app.
So while running the app from your business logic, any where if you use
db value it will read from above export irrespective of NODE_ENV.
After exporting all required environment variable, run your application using
If everything is fine you should be able to see your application up and running using the profile specific information you specified.
For example application please check in GitLab.
Thats it…! Thanks for reading, If you find this information useful, please go ahead and give some claps.