简体   繁体   中英

Manage config in nodejs to get variables from file and env variables

I have a configuration file in which passwords are stored and the data are used

'use strict';

export default {
    dbProd: {
        connectionString: 'postgres://...',
    },
    dbDev: {
        connectionString: 'postgres://...',
    },
    ...
}

I also have a postgresql database connection file

'use strict';

import { Pool } from 'pg';
import config from './../config';

export default new Pool({
    connectionString: config.dbDev.connectionString,
    ssl: true
});

In my package.json file, there are three scripts. The first to launch the project, the second to develop, and the third for the production

  "scripts": {
    "start": "nodemon --exec babel-node src/index.js",
    "devbuild": "babel src --out-dir dev-backend",
    "build": "babel src --out-dir backend"
  },

I need it when I collect a project for production to connect to the database via dbProd , and if the development was transferred to dbDev . How to implement it?

You should consider to use the node-package config NPM Config , because it makes these things much easier.

You would just need to create a default.json file inside a /config folder, which would include all of your development configs and configs that do not change either in development nor in production. The second file which you have to create would be a production.json files that includes all your production configs that overrides the default key:values.

In your package.json you simply need a start-script for the production enviroment and one for the development. This could look like this:

 "start": "SET NODE_ENV=production& node ./bin/www",
 "dev": "node ./bin/www",

By setting NODE_ENV=production the config module would load the default config and look inside the production-file to find out which values must be override in production mode.

A simple example of the default config file :

{
    "Services": {
      "api": {
        "username": "user",
        "password": "password",
        "base_url": "https://development.com/test"
      }
    },
    "Network": {
      "proxy": {
        "host": "http://myproxy/",
        "port": 80
      }
    }
  }

In case the proxy is always the same and just the API changes, the production file would look like this:

  {
    "Services": {
      "api": {
        "username": "prodUser",
        "password": "prodPassword",
        "base_url": "https://production.com/test"
      }
    }
  }

This can now be used in your code like this for example:

const config = require('config');
const serviceConfig = config.get('Services');
const networkConfig = config.get('Network');

networkConfig.get('proxy.host')+":"+networkConfig.get('proxy.port')

You need to use config package or any other popular packages that allow you to define different configs for every environment.

Using config you'll have to make the /config folder where you can put all your configs per environment like following:

/config/development.json
/config/production.json

Usage:

 var config = require('config');
 //...
 var dbConfig = config.get('Customer.dbConfig');
 db.connect(dbConfig, ...);

 if (config.has('optionalFeature.detail')) {
   var detail = config.get('optionalFeature.detail');
   //...
 }

This package uses NODE_ENV environment variable value to determine which config to use so you'll have to set this variable before starting the application.

$ export NODE_ENV=production
$ node my-app.js

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM