Node.js设置要与Everyauth一起使用的环境特定配置 [英] Node.js setting up environment specific configs to be used with everyauth

查看:137
本文介绍了Node.js设置要与Everyauth一起使用的环境特定配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js + express.js + everyauth.js。我把我所有的每一个逻辑都移动到一个模块文件中。

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'
}

这些代码对于不同的环境是不同的 - 开发/分段/生产,因为回调是不同的网址。

These codes are different for different environments - development / staging / production as the callbacks are to different urls.

曲。如何在环境配置中设置这些以过滤所有模块,或者我可以将路径直接传递到模块中?

Qu. How do I set these in the environmental config to filter through all modules or can I pass the path directly into the module?

设置在env:

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'});
});

?希望这是有道理的

推荐答案

我的解决方案,

使用

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解决方案加载文件并创建一个新的实例,我们可以传递一个值if在这种情况下, 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一起使用的环境特定配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆