簡體   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