How to use JSON based configuration in Express JS

Krishna KV
Technology Specialist
June 13, 2018
Rate this article
[Total: 5    Average: 4.4/5]
Views    682

Every application has environment based configuration and Express Js is no exception from that. This article explains how to manage these environment (qas,dev,prod) specific configuration in Express Js with JSON files.

As we need to change the configuration information such as database, port,etc. I have used the nconf package to read the configuration information, nconf helps us to store these information in memory.

As a first step, create enum of environments in a file called config/environment.ts. This will be used in the AppSetting.ts

export enum Environment {
    Dev, Production, Local
}

Configuration json can be in any structure that confirms to json format, below is an example of configuration.

{
  "Config": {
    "port": 40401,
    "appConfig": {
      "version": "1.0.0",
      "name": "express-api",
      "env": "development"
    }
  }
}

The file, config/configmanager.ts , is used to read the configuration file based on the environment and store in the memory using the method nconf.use(‘memory’). The memory can be cleared using the method nconf.clear();

This Express application is created with TypeScript. If you would like to know how to set up a Express Js application with TypeScript, check out my previous article Express Js Application Seed Project With TypeScript
export class ConfigManager {
    public Config: IConfig;
    constructor(pth?: string) {
        let filename;
        switch (AppSetting.Env) {
            case Environment.Dev:
            case Environment.Local:
                filename = 'config.dev.json';
                break;
            case Environment.Production:
                filename = 'config.prod.json';
                break;
            default:
                Logger.error('Unable to read the config file');
                process.exit();
                break;
        }
        nconf.use('memory');
        if (!nconf.get('Config')) {
            this.getFile(filename);
        }
        this.Config = nconf.get('Config');
        if (!this.Config) {
            Logger.error('Unable to read the config file');
            process.exit();
        }
    }
    public getFile(filename) {
        nconf.file('Config', {
            file: filename,
            dir: './config/',
            search: true
        });
        if (!nconf.get('Config')) {
            nconf.file('Config', {
                file: 'config/' + filename,
                dir: __dirname,
                search: true
            });
        }
    }
    public reset() {
        nconf.reset();
        nconf.clear();
    }
}

Based on the environment in the app.setting.ts, the configuration file will get loaded. The copy webpack plugin will copy all the configuration files to the dist folder, which will help to deploy the application.

export class AppSetting {
    public static Env = Environment.Dev;

    public static getConfig(): IConfig {
        let configManager = new ConfigManager();
        return cloneDeep(configManager.Config);
    }
}

Click here to download the project from Github.

Subscribe To Our Newsletter

Leave a comment