我一直在开发一些Node应用程序,我一直在寻找一种存储部署相关设置的良好模式。在Django世界(我来自那里),常见的做法是有一个settings.py文件包含标准设置(时区等),然后有一个local_settings.py用于部署特定的设置,即。要与什么数据库通信、什么memcache套接字、管理员的电子邮件地址等等。

我一直在为Node寻找类似的模式。只要一个配置文件就好了,这样它就不必与app.js中的其他所有东西挤在一起,但我发现有一种方法在源代码控制之外的文件中拥有特定于服务器的配置很重要。同一款应用可以部署在不同设置的服务器上,必须处理合并冲突,这不是我的乐趣所在。

那么是否存在某种框架/工具,或者每个人都只是自己拼凑一些东西?


当前回答

我在这里尝试了一些建议的解决方案,但对它们不满意,所以我创建了自己的模块。它被称为微配置,主要的区别是它尊重约定而不是配置,所以你可以只需要这个模块并开始使用它。

你存储你的配置在纯js,或json文件从/config文件夹。首先它加载default.js文件,然后是/config目录下的所有其他文件,然后它根据$NODE_ENV变量加载特定于环境的配置。

它还允许使用local.js或特定于环境的/config/env/$NODE_ENV.local.js覆盖本地开发的配置。

你可以在这里看看:

https://www.npmjs.com/package/mikro-config

https://github.com/B4nan/mikro-config

其他回答

对于那些正在访问这个旧线程的人来说,这里有一个我觉得不错的包。

https://www.npmjs.org/package/config

只需使用npm模块配置(超过300000次下载)

https://www.npmjs.com/package/config

节点配置为应用程序部署组织分层配置。

它允许您定义一组默认参数,并为不同的部署环境(开发、qa、登台、生产等)扩展它们。

$ npm install config
$ mkdir config
$ vi config/default.json


{
      // Customer module configs
      "Customer": {
        "dbConfig": {
          "host": "localhost",
          "port": 5984,
          "dbName": "customers"
        },
        "credit": {
          "initialLimit": 100,
          // Set low for development
          "initialDays": 1
        }
      }
}



$ vi config/production.json

{
  "Customer": {
    "dbConfig": {
      "host": "prod-db-server"
    },
    "credit": {
      "initialDays": 30
    }
  }
}



$ vi index.js

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

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


$ export NODE_ENV=production
$ node index.js

我将创建一个文件夹,配置一个命名为config.js的文件,稍后我会在需要的地方使用这个文件,如下所示

config.js的例子

module.exports = {
    proxyURL: 'http://url:port',
    TWITTER: {
        consumerkey: 'yourconsumerkey',
        consumerSecrete: 'yourconsumersecrete'
    },
    GOOGLE: {
        consumerkey: 'yourconsumerkey',
        consumerSecrete: 'yourconsumersecrete'
    },
    FACEBOOK: {
        consumerkey: 'yourconsumerkey',
        consumerSecrete: 'yourconsumersecrete'
    }
}

然后,如果我想在某处使用这个配置文件

我将首先导入如下

Var config = require('./config');

,我可以访问如下的值

const oauth = OAuth({
    consumer: {
        key: config.TWITTER.consumerkey,
        secret: config.TWITTER.consumerSecrete
    },
    signature_method: 'HMAC-SHA1',
    hash_function(base_string, key) {
        return crypto.createHmac('sha1', key).update(base_string).digest('base64');
    }
});

很久以后,我发现了一个非常好的Node.js模块来管理配置:nconf。

举个简单的例子:

var nconf = require('nconf');

// First consider commandline arguments and environment variables, respectively.
nconf.argv().env();

// Then load configuration from a designated file.
nconf.file({ file: 'config.json' });

// Provide default values for settings not provided above.
nconf.defaults({
    'http': {
        'port': 1337
    }
});

// Once this is in place, you can just use nconf.get to get your settings.
// So this would configure `myApp` to listen on port 1337 if the port
// has not been overridden by any of the three configuration inputs
// mentioned above.
myApp.listen(nconf.get('http:port'));

它还支持在Redis中存储设置,编写配置文件,并且有一个相当可靠的API,并且还得到了一个更受尊敬的Node.js商店Nodejitsu的支持,作为Flatiron框架计划的一部分,所以它应该是相当经得起未来的。

在Github上查看nconf。

只需要用exports做一个简单的settings.js:

exports.my_password = 'value'

然后,在你的脚本中,执行require:

var settings = require('./settings.js');

所有的设置现在将可用的设置变量:

settings.my_password // 'value'