javascript - Best way to use a configuration file in Node.JS - Stack Overflow

I have a configuration file (simplified below) for a Node.JS appmodule.exports = function(){var setting

I have a configuration file (simplified below) for a Node.JS app

module.exports = function(){
  var settings = {
    port: '8088'
  }; 
  settings.mysql = {
    host : 'localhost',
    database : 'test'
  };
  // Override default settings
  switch(process.env.NODE_ENV){
    case 'production':
      settings.port = 8082;
    break;
    case 'staging':
      settings.port = 8083;
    break;     
  }
  return settings;
};

When I start my Express.js application I require this file for some basic settings:

var Config = require('./config'),  settings = new Config();
var port = process.env.PORT || settings.port;    // set our port

I also need to use the MySQL settings in this file later on within a DAO(in my model). At that point I call the configuration file (which will run it again)

var Config = require('../config'), settings = new Config();
var mysql = require('mysql');
var pool = mysql.createPool(settings.mysql);

Obviously each time I "require" the file gets run, this just seem lazy/inefficient. Should I be storing the returned "settings" variable in a global variable which my DAO can see, or should I be passing it in by reference?

I did at one point think about making it middleware and add it the REQUEST but then I would need my route (controller) to all pass it in to the DAO (model) which doesn't feel right.

I have a configuration file (simplified below) for a Node.JS app

module.exports = function(){
  var settings = {
    port: '8088'
  }; 
  settings.mysql = {
    host : 'localhost',
    database : 'test'
  };
  // Override default settings
  switch(process.env.NODE_ENV){
    case 'production':
      settings.port = 8082;
    break;
    case 'staging':
      settings.port = 8083;
    break;     
  }
  return settings;
};

When I start my Express.js application I require this file for some basic settings:

var Config = require('./config'),  settings = new Config();
var port = process.env.PORT || settings.port;    // set our port

I also need to use the MySQL settings in this file later on within a DAO(in my model). At that point I call the configuration file (which will run it again)

var Config = require('../config'), settings = new Config();
var mysql = require('mysql');
var pool = mysql.createPool(settings.mysql);

Obviously each time I "require" the file gets run, this just seem lazy/inefficient. Should I be storing the returned "settings" variable in a global variable which my DAO can see, or should I be passing it in by reference?

I did at one point think about making it middleware and add it the REQUEST but then I would need my route (controller) to all pass it in to the DAO (model) which doesn't feel right.

Share Improve this question asked Feb 5, 2016 at 9:23 Andy JarrettAndy Jarrett 8733 gold badges9 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

Just export the object itself instead of wrapping it in a function - Node modules get cached after the first load, so it won't run the logic again.

var settings = {
  port: '8088'
}; 

settings.mysql = {
  host : 'localhost',
  database : 'test'
};

// Override default settings
switch(process.env.NODE_ENV){
  case 'production':
    settings.port = 8082;
  break;
  case 'staging':
    settings.port = 8083;
  break;     
}

module.exports = settings;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742352103a4427723.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信