繁体   English   中英

节点配置多个配置文件

[英]node-config multiple configuration files

我正在看这个https://github.com/lorenwest/node-config ,它似乎可以完成我需要的一切。 但是我想知道是否有可能根据类别指定多个配置文件。

这可以做到吗?

./config
   default-aws.json or aws-default.json
   production-aws.json or aws-production.json
   db-default.json
   db-production.json 

等等..

所以配置文件可以更小? 我知道我们可以制作一个巨大的配置,其中包含不同部分中所需要的全部配置。 例如

{
   "aws": {
      "kinesis": "my-stream"
       ....
    },
    "db": {
       "host": "my-host"
        ...
    }
}

任何人都可以使用node-config或类似于node-config的其他库来实现此想法吗?

谢谢与问候田

最简洁的答案是不。 node-config不支持此功能(作为@Mark的响应)。

使用node-config进行此操作的最简单方法是将JavaScript用作配置文件( https://github.com/lorenwest/node-config/wiki/Configuration-Files#javascript-module---js )。 这样,您仍然可以获得使用node-config的大部分好处。 然后,您可以在其中简单地包含其他文件( https://github.com/lorenwest/node-config/wiki/Special-features-for-JavaScript-configuration-files#includes-other-files

请注意,使用多个配置文件和覆盖内部文件会有些困难。

稍微复杂一点的实现可以在config中使用实用程序类,该实用程序类实际上允许您使用node-config在内部使用的相同模式直接读取特定文件夹( https://github.com/lorenwest/node-config/wiki/Using -Config-Utilities#loadfileconfigsdirectory )。 在这种情况下,您可能希望将所有文件组合到一个配置中,方法是在第一次调用get( https://github.com/lorenwest/node-config/wiki/Configuring-from- an-External-Source )。

我使用nconf 它使您可以将多个配置文件读入同一对象。 这是一个例子:

var nconf = require('nconf');

//read the config files; first parameter is a required key
nconf.file('aws', {file: 'default-aws.json'});
nconf.file('db', {file: 'db-default.json'});

console.log(nconf.get('aws:kinesis'));
console.log(nconf.get('db:host'));

default-aws.json:

{
  "aws": {
    "kinesis": "my-stream"
  }
}

db-default.json:

{
  "db": {
    "host": "my-host"
  }
}

我是node-config的维护者。 下一版本将支持声明多个NODE_ENV值(以逗号分隔)的功能。

因此,如果您要在云中进行开发,则可以先声明一个“开发”环境,再声明一个“云”环境。

首先将加载开发配置,然后是“ cloud”配置。

相关的拉取请求是#486

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM