admin管理员组文章数量:1333159
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 badges1 Answer
Reset to default 5Just 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;
本文标签: javascriptBest way to use a configuration file in NodeJSStack Overflow
版权声明:本文标题:javascript - Best way to use a configuration file in Node.JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742352102a2458700.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论