confman 是一个强大的配置文件加载器,不管你喜欢 yaml 、cson、json、properties、plist、ini、toml、xml 仍是 js,都能知足你的愿望,而且更加简单、更增强大。node
$ npm install confman --save
目录git
app ├── index.js ├── config.dev.yaml ├── config.prod.yaml └── config.yaml
index.jsgithub
const confman = require('confman'); const configs = confman.load(`${__dirname}/config`); console.log(configs);
启动应用npm
$ NODE_ENV=prod node index.js
经过指定 NODE_ENV
能够加载指定的「环境配置文件 config.prod.yaml」,并和「默认配置 config.yaml」进行合并, 若是有相同的配置,「环境配置会覆盖默认配置」json
文件一: test1.yamlmvc
name: test1 #能够使用 $require 引用其它文件 child: $requrie ./test2
文件二: test2.jsonapp
{ "name": "test2", "child": "$require other-file" }
$require
能够在任意支持的格式的配置文件中使用测试
目录结构ui
├── config │ ├── conn.yaml │ ├── index.yaml │ └── mvc.yaml ├── config.dev │ └── conn.yaml ├── config.prod │ └── conn.yaml └── index.js
index.jscode
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'));
方式一,映射到一个已经支持(指已注册的 loader)的 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