1

我正在查看此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 

ETC..

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

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

如果使用 node-config 或与 node-config 类似的其他库可以做到这一点,任何人都有任何想法?

谢谢和问候锡

4

3 回答 3

3

最简洁的答案是不。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#include-other-files

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

稍微复杂一点的实现可以使用 config 中的实用程序类,它实际上允许您使用 node-config 内部使用的相同模式直接读取特定文件夹(https://github.com/lorenwest/node-config/wiki/Using -Config-Utilities#loadfileconfigsdirectory)。在这种情况下,您可能希望通过在第一次调用获取之前将所有文件设置在配置对象上将它们组合到一个配置中(https://github.com/lorenwest/node-config/wiki/Configuring-from-一个外部来源)。

于 2018-06-04T19:25:16.220 回答
2

I use nconf. It lets you read multiple configuration files into the same object. Here is an example:

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"
  }
}
于 2018-03-09T19:33:30.637 回答
1

我是node-config. 下一个版本将支持声明多个NODE_ENV值的功能,以逗号分隔。

因此,如果您在云中进行开发,您可以声明一个“开发”环境,然后是一个“云”环境。

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

相关的拉取请求是#486

于 2018-06-05T12:50:42.687 回答