Configuring Express with Nconf

Nconf is a configuration tool that we will use to create hierarchical/environment configuration files for our application. Let's install Nconf:

npm install nconf --save

The first thing we will do is to move the following hardcoded port number from our Express application into our configuration:

app.set('port', 3000);

Let's create the module ./lib/configuration/index.js, which will allow us to to read configuration data from JSON files. We import the nconf module and define a constructor function, Config. We then load a configuration file based on the current environment and load the default configuration that holds non-environmental configuration data. We also define a function get(key), which accepts a key and returns ...

Get Advanced Express Web Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.