我正在看这个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的库可以做到这一点?
感谢和问候Tin
发布于 2018-06-05 03:25:16
简短的回答是否定的。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#including-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)之前在配置对象上设置它们,从而将所有文件组合到一个配置中。
发布于 2018-03-10 03:33:31
我使用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'));默认-aws.json:
{
"aws": {
"kinesis": "my-stream"
}
}db-default.json:
{
"db": {
"host": "my-host"
}
}发布于 2018-06-05 20:50:43
我是node-config的维护者。下一个版本将支持声明多个用逗号分隔的NODE_ENV值的功能。
因此,如果您在云中进行开发,您可以先声明一个“开发”环境,然后再声明一个“云”环境。
首先加载开发配置,然后加载"cloud“配置。
相关的拉取请求为#486。
https://stackoverflow.com/questions/49191228
复制相似问题