In your Express applications, there is usually a need to set different configuration values for connecting to a database, getting the port number for your server to run on and even just saving API secrets. It is possible to do all this with a .env
file but when your application grows this becomes cumbersome and difficult to manage. That is why I will be introducing you to a node package called 'config' which makes it easy to manage your configuration variables and access them easily from your application.
Setup
First of all, make sure you have Node running on your computer and in your project directory install the config
package using this command:
npm i config
Then create a folder called config
in that same directory. This is where all your config files will be saved.
mkdir config
touch default.json
All the common configurations will be stored in default.json
and you can create specific configuration files for different environments e.g production.json
, development.json
. Also, there is a special file type called custom-environment-variable.json
which can load environment variables, we will have a look at this later in this guide.
Usage
Let’s consider that we have a simple application where we are to provide database configuration details both for our production and development environments. Then on the server, the port is to be provided from an environment variable
. In our config directory, we have 4 files (default.json, production.json, development.json, custom-environment-variables.json) with the following content:
default.json
{
"dbConfig": {
"host": "localhost",
"port": "3607",
"dbName": ""
},
"app":{
"port" : ""
}
}
development.json
{
"dbConfig": {
"host": "",
"port": "",
"dbName": "customers"
},
"app":{
"port" : ""
}
}
custom-environment-variables.json
{
"dbConfig": {
"host": "",
"port": "",
"dbName": ""
},
"app":{
"port" : "PORT"
}
}
Note that “PORT” will be read from the environment variables.
Now that we have set all our configuration data, let’s now use it in our application. In index.js:
const express = require('express');
const app = express();
const mongoose = require('mongoose');
//This cofig variable is what we will use to access our config data
const config = require('config');
const dbHost = config.get('dbConfig.host');
const dbPort = config.get('dbConfig.port');
const dbName = confit.get('dbConfig.dbName');
mongoose.connect(`mongodb://${dbHost}:${dbPort}/${dbName}`);
app.get('/', (req, res)=>{
res.send("Hello World");
})
if(config.has('app.port')){
const port = config.get('app.port');
app.listen(port);
} else{
console.log('Cannot start server without a port number');
}
From the code example above, config.get('variable.name')
is used to get a specific value from the configuration file and it uses the dot notation to access nested properties, meanwhile config.has('app.port')
is used to check if a property is set and will return true if is set and false otherwise. Note that this is a very basic explanation of this package as this article was just meant to provide the essentials, there is more to it than I have covered here.