Confman - 针对「Node 应用」的配置文件加载模块 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
houfeng

Confman - 针对「Node 应用」的配置文件加载模块

  •  
  •   houfeng 2016 年 7 月 26 日 2733 次点击
    这是一个创建于 3559 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一句话介绍

    confman 是一个强大的配置文件加载器,无论你喜欢 yaml 、 cson 、 json 、 properties 、 plist 、 ini 、 toml 、 xml 还是 js ,都能满足你的愿望,并且更加简单、更加强大。

    npm version Build Status

    支持的特性

    • 支持多种配置文件格式,默认包括 yaml/cson/json/properties/plist/ini/toml/xml/js
    • 支持配置文件相互引用,无论何种格式都可以「引用其它任意格式」的配置文件
    • 支持「基于目录」的多文件配置
    • 支持「环境配置」,区分加载生产、测试等不同的配置
    • 可以非常方便的「扩展」新的配置文件格式
    • 可以「混合使用」不同的配置文件格式

    现在就安装

    $ npm install confman --save 

    来几个示例

    不同的环境配置

    目录

    app ├── index.js ├── config.dev.yaml ├── config.prod.yaml └── config.yaml 

    index.js

    const cOnfman= require('confman'); const cOnfigs= confman.load(`${__dirname}/config`); console.log(configs); 

    启动应用

    $ NODE_ENV=prod node index.js 

    通过指定 NODE_ENV 可以加载指定的「环境配置文件 config.prod.ymal 」,并和「默认配置 config.yaml 」进行合并, 如果有相同的配置,「环境配置会覆盖默认配置」

    配置文件相互引用

    文件一: test1.yaml

    name: test1 #可以使用 $require 引用其它文件 child: $requrie ./test2 

    文件二: test2.json

    { "name": "test2", "child": "$require other-file" } 

    $require 可以在任意支持的格式的配置文件中使用

    基于目录的多文件配置

    目录结构

    ├── config │ ├── conn.yaml │ ├── index.yaml │ └── mvc.yaml ├── config.dev │ └── conn.yaml ├── config.prod │ └── conn.yaml └── index.js 

    index.js

    const cOnfman= require('confman'); const cOnfigs= confman.load(`${__dirname}/config`); console.log(configs); 

    扩展新格式

    其实,多数情况你不需要这么做,如果确实有需要,你可这样编写一个自定义 loader

    module.exports = { extname: '.xxx', load: function (configPath) { //... return configs; } }; 

    添加自定义 loader

    confman.loaders.push(require('your-loader-path')); 

    自定义扩展名

    方式一,映射到一个已经支持(添加注册到 confman )的 loader

    confman.loaders.push({ extname: '.xxx', loader: '.yaml' }); 

    方式二,映射到一个自定义 loader

    confman.loaders.push({ extname: '.xxx', loader: require('your-loader-path') }); 

    现在或来有可能会用到?那你应该去加个 Star
    GitHub : https://github.com/Houfeng/confman

    1 条回复    2016-07-26 23:40:56 +08:00
    phoenixlzx
        1
    phoenixlzx  
       2016 年 7 月 26 日
    star 已送
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2777 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 44ms UTC 01:21 PVG 09:21 LAX 18:21 JFK 21:21
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86