Menu Close

Effortlessly manage configuration data in your Node.js microservices with nconf

Effortlessly manage configuration data in your Node.js microservices with nconf

Building microservices requires careful attention to detail in the area of configuration management. It will be much simpler for you to launch and maintain your services as a result of this ability to segregate configuration data from code. In this post, we’ll take a look at how to manage configuration data in Node.js microservices by using the nconf package.

Why Should You Use nconf?

There are several good reasons why you should consider using nconf as the configuration management tool for your Node.js microservices, including the following:

  • Flexibility: nconf offers you the ability to load configuration data from a wide number of sources, including command-line arguments, environment variables, a configuration file, and default settings. Because of this, it is simple to modify the behaviour of your microservices in accordance with the environment they are running in
  • Ease of use: nconf’s application programming interface (API) is quite straightforward, which makes retrieving and setting configuration data very simple. Additionally, it supports hierarchical data structures, which enables you to store and retrieve configuration settings that are nested within one another
  • Well documented: because it is so well documented, both learning it and putting it to use won’t be difficult at all, thanks to nconf’s extensive documentation and several working examples.


Here is an example of how you might use nconf in a Node.js microservice:

const express = require('express');
const nconf = require('nconf');

// Initialize nconf to use (in-order):
//   1. Command-line arguments
//   2. Environment variables
//   3. A configuration file
//   4. Default values
nconf.argv().env().file({ file: 'config.json' }).defaults({
  port: 3000,
  host: 'localhost'

const app = express();

// Get the value of the 'port' key from the configuration data
const port = nconf.get('port');

app.listen(port, () => {
  console.log(`Microservice listening on port ${port}`);

This piece of code generates an Express app and makes use of nconf to load configuration data from a configuration file, command-line arguments, environment variables, and other sources, in addition to using default values. After that, the get() method is used in order to acquire the value of the port key from the configuration data, and it then begins the server process on the port that was given.

After that, you will be able to add routes to the application, which will enable customers to interact with the microservice as required. As an illustration, you can decide to build a route in order to obtain the currently active configuration data:

app.get('/config', (req, res) => {

This route retrieves all of the configuration data by utilizing the get() method, and then gives it back to the client in the form of a JSON object.

View Source
Posted in Node.js