node.js项目配置文件工具

bright-config

源码: github.com/sikichan/br…

在配置文件夹 config 目录下,必须有一个default文件夹存放项目运行的默认配置文件(.js)。因为项目运行在不一样的环境,好比开发环境、测试环境和生产环境,会使用到不一样的配置信息,所以能够在 config 目录下创建运行环境文件夹,例如 development, test, production。git

bright-config工具能够根据项目当前运行环境(环境变量 NODE_ENV),把环境配置目录(例如 test 目录)下对应 default 目录下的同名 .js 文件导出的对象属性进行覆盖,对 default 配置对象里没有的对象和属性,会进行添加整合,最后返回一个新的配置对象。github

项目结构例子

运行时没指定环境变量NODE_ENV,则默认为开发环境(development)
复制代码

运行时指定环境为生产环境(production)
process.env.NODE_ENV=production
复制代码

安装

使用 npm 安装npm

npm install bright-config
复制代码

使用例子

const path = require('path')
const brightConfig = require('bright-config')

const config = brightConfig(path.join(__dirname, './config')) // config配置目录的路径

console.log(config)
复制代码

config目录例子bash

----config
--------default // 必须存在默认配置目录
------------app.js
------------db.js
------------oss.js
--------development
------------app.js
------------db.js
------------oss.js
--------production
------------app.js
------------oss.js
--------test
------------app.js
复制代码
相关文章
相关标签/搜索