0
experienceList:
  - dbAnalyst: &dbAnalyst
      posName: 'Database Analyst / Net Tech'
      companyName: 'Choices People Supporting People'
  - webDevXetex: &webDevXetex
      posName: 'Software Engineer'
      companyName: 'Xetex Business Systems'
# ...
experienceStrategies:
  - defaultExperience: &defaultExperience
    - <<: *dbAnalyst
    - <<: *webDevXetex
  - webDevExperience: &webDevExperience
    - <<: *webDevXetex
#...
experience: *defaultExperience

配置/default.yml

这导致experience在 YAML 中输出为:

experience: 
  - dbAnalyst:
      posName: 'Database Analyst / Net Tech'
      companyName: 'Choices People Supporting People'
  - webDevXetex:
      posName: 'Software Engineer'
      companyName: 'Xetex Business Systems'

现在我想做的是,保留默认值并覆盖它们,在类似于以下的覆盖文件中config/default.yml选择不同的策略(上述之一):experienceStrategies:

experience: *webDevExperience

配置/覆盖.yml

并希望产生以下结果:

experience: 
  - webDevXetex:
      posName: 'Software Engineer'
      companyName: 'Xetex Business Systems'

...

但是,使用 node-config 库时,我会遇到错误;有没有办法在 yaml 和 node.js 中使用其他库或坚持使用 node-config 来做到这一点?我见过另一个名为node-config-yaml 的库,它允许您包含其他 yaml 文件,但不确定它是否允许您以使用node-config.

/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:933
    throw new Error("Cannot parse config file: '" + fullFilename + "': " + e3);
    ^

Error: Cannot parse config file: '/home/leeand00/Documents/lifehacker organized/docs/projdir/config/company_job.yml'
: YAMLException: unidentified alias "webDevExperience" at line 14, column 30:
    experience: *webDevExperience
                                 ^
    at util.parseFile (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:
933:11)
    at /home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:702:28
    at Array.forEach (native)
    at /home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:698:14
    at Array.forEach (native)
    at util.loadFileConfigs (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/conf
ig.js:697:13)
    at new Config (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:122:
27)
    at Object.<anonymous> (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config
.js:1767:31)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
4

1 回答 1

0

使用该node-config-yml库实际上起到了作用,尽管它关于此事的文档很少,但它可以完美地工作。

似乎将配置从它们两个都放在内存中的一个文件中。您可以使用别名和合并功能,node-config并根据需要覆盖。

于 2018-01-12T18:40:27.707 回答