问题描述
我正在使用 node.js + express.js +everyauth.js.我已将所有的everyauth 逻辑移到一个模块文件中
I am using node.js + express.js + everyauth.js. I have moved all my everyauth logic into a module file
var login = require('./lib/everyauthLogin');
在其中我使用密钥/秘密组合加载我的 oAuth 配置文件:
inside this I load my oAuth config file with the key/secret combinations:
var conf = require('./conf');
.....
twitter: {
consumerKey: 'ABC',
consumerSecret: '123'
}
这些代码对于不同的环境是不同的 - 开发/登台/生产,因为回调是不同的 url.
These codes are different for different environments - development / staging / production as the callbacks are to different urls.
问题:如何在环境配置中设置这些以过滤所有模块,或者我可以将路径直接传递到模块中吗?
Question: How do I set these in the environmental config to filter through all modules or can I pass the path directly into the module?
在环境中设置:
app.configure('development', function(){
app.set('configPath', './confLocal');
});
app.configure('production', function(){
app.set('configPath', './confProduction');
});
var conf = require(app.get('configPath'));
传入
app.configure('production', function(){
var login = require('./lib/everyauthLogin', {configPath: './confProduction'});
});
?希望这是有道理的
推荐答案
我的解决方案,
My solution,
使用加载应用程序
NODE_ENV=production node app.js
然后将 config.js
设置为函数而不是对象
Then setup config.js
as a function rather than an object
module.exports = function(){
switch(process.env.NODE_ENV){
case 'development':
return {dev setting};
case 'production':
return {prod settings};
default:
return {error or other settings};
}
};
然后根据 Jans 解决方案加载文件并创建一个新实例,如果需要,我们可以传入一个值,在这种情况下 process.env.NODE_ENV
是全局的,因此不需要.
Then as per Jans solution load the file and create a new instance which we could pass in a value if needed, in this case process.env.NODE_ENV
is global so not needed.
var Config = require('./conf'),
conf = new Config();
然后我们可以像以前一样访问配置对象属性
Then we can access the config object properties exactly as before
conf.twitter.consumerKey
这篇关于Node.js 设置与everyauth 一起使用的环境特定配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!