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 config .


1. Create a nodejs application:-

  • Create a folder and go to the folder and run npm init and enter all the values it asks.
  • Install config module by running the commandnpm install -S config .
  • Basically config module requires to keep all profile data in a common folder. By default it reads from a folder called config from 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 config in my application directory.

2. specify your configurations in profile files:-

  • Once you create config folder you can specify your profile information for each stage in files.
  • config will support multiple file formats like .json, .json5, .ts, .yaml, .coffee, .properties, .cson, .toml etc. I use .json file.
  • config requires to pass a environment variable like NODE_ENV while running your Node.js app. If you forgot to set NODE_ENV while running the app, config reads the configurations from the default configuration.
  • Specify a default.json in config directory. After default.json is provided, add different stage configurations in files like dev.json, test.json, qa.json, pre-prod.json, prod.json as per your stages.
  • If you have multiple instances in each stage you can even specify the config files like {stage-name}-{instance}.{extension} .
  • Please find the sample profile file as shown below.
Sample JSON profile file demonstrating stage specific keys.

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.
Sample code for reading profile information.

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_ENV variable. This name should be same as the profile file name but without file extension.
export NODE_ENV=dev
  • This means you are asking config module to read the keys from dev.json
  • 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 NODE_APP_INSTANE=1 along with NODE_ENV=dev
export NODE_ENV=dev
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.

export NODE_CONFIG='{"db":{"Connection":"mysql://root:root@localhost:3306/sample-schema"}}' 

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 node your-filename.js

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.

walkin

Retail Interactions Reimagined. Ping us at info@getwalk.in if you want to know more…

Saikrishna Dronavalli

Written by

L'earner' by birth.

walkin

walkin

Retail Interactions Reimagined. Ping us at info@getwalk.in if you want to know more…

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade