How to convert / merge configuration files in node.js / nconf?

I have a node.js file and a conf.json file with application settings, that is:

{
  "settings": [
    {
      "name": "setting1",
      "connectionString": {
        "host": "mongodb://127.0.0.1:27017/db",
        "collection": "collection1"
      }
    }, 
    {
      "name": "setting2",
      "file": "/path/file",
      "token": "development token"
    }
  ]
}

Is there a way in nconf or another tool to have an analogue of the .NET configuration conversion, so I can have a production configuration file with overrides, i.e. conf.production.json:

{
  "settings": [
    {
      "name": "setting2",
      "token": "production token"
    }
  ]
}

The expected value of the parameter "setting2" in production mode is "production token" and the rest is from the default configuration file. I tried loading the base files using nconf, but it does not work:

nconf.file(process.env.NODE_ENV, './conf.' + process.env.NODE_ENV + '.json');
nconf.file('./conf.json');
+4
source share
1 answer

The relevant part of my script configuration, which I personally use for this:

nconf
    .argv    ()
    .env     ()
    .defaults( require( './_config.js' ) )
    .use     ( 'memory' )
    ;

...

nconf.set( 'env'     , nconf.get( 'environments:' + env ) );
nconf.set( 'env:name', env );

var includes = nconf.get( 'env:includes' );

if( {}.toString.call( includes ) === '[object Array]' ){
    var incs = {};

    includes.forEach( function( val ){
        incs[ val ] = val;
    } );

    includes = incs;
}

Object.keys( includes ).forEach( function( incName ){
    var incPath = includes[ incName ]
      , incData = nconf.get( 'environments:' + incPath );

    Object.keys( incData ).forEach( function( key ){
        var keyNm = ( incName.indexOf( '__local' === 0 ) )
            ? 'env:%s'   .sprintf( key )
            : 'env:%s:%s'.sprintf( incName, key )
            ;

        if( nconf.get( keyNm ) == null ){
            nconf.set( keyNm, incData[ key ] );
        }
    });
} );

, sprintf, npm .

, env .env . includes JSON .

config.get( 'env:<param>' )

config.js ( .json, ), :

module.exports =

{
    environments : {
        libraries : {
            jQuery : '//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js'
        }

        , localLibraries : {
            jQuery : '/js/jquery-2.1.0.js'
        }

        , web_server : {
                      task : 'web_server'
            ,     includes : [ 'cdn', 'libraries' ]

            ,         port : 12567
            , csrfMaxAgeMs : 1000 * 60 * 60 * 24 * 30 // 30 days
            ,  csrfHashAlg : 'sha256'

            ,  apiHostName : '(^|\\.)api.*'

            ,     frontend : '/frontend'

            ,   jsonSpaces : 0
        }

        , local_web_server : {
                     task : 'web_server'
            ,    includes : { __local: 'web_server', libraries: 'localLibraries' }
            ,  jsonSpaces : 4
            ,         dev : true
        }
    }
};

(['cdn', 'libraries']), , , env.cdn.param. , __local, .

+1

All Articles