node-config/node-config
GitHub: node-config/node-config
Node.js 应用分层配置管理库,通过多环境配置文件合并和多种覆盖机制,解决应用在不同部署环境下的配置组织和隔离问题。
Stars: 6425 | Forks: 510
# 配置你的 Node.js 应用
[](https://www.npmjs.com/package/config)
[](https://www.npmjs.com/package/config)
[](https://github.com/node-config/node-config/issues)
[发布说明](https://github.com/node-config/node-config/releases)
## 简介
Node-config 为你的应用部署组织分层配置。
它允许你定义一组默认参数,
并针对不同的部署环境(development, qa,
staging, production 等)进行扩展。
配置存储在你的应用程序内的[配置文件](https://github.com/node-config/node-config/wiki/Configuration-Files)中,并且可以被[环境变量](https://github.com/lorenwest/node-config/wiki/Environment-Variables)、
[命令行参数](https://github.com/node-config/node-config/wiki/Command-Line-Overrides)或[外部源](https://github.com/lorenwest/node-config/wiki/Configuring-from-an-External-Source)覆盖和扩展。
这为你的应用程序提供了一致的配置接口,该接口由同样使用 node-config 的[不断增长的 npm 模块列表](https://www.npmjs.org/browse/depended/config)共享。
## 项目准则
* *简单* - 快速上手
* *强大* - 支持多节点企业级部署
* *灵活* - 支持多种配置文件格式
* *轻量* - 文件小,内存占用低
* *可预测* - 为模块和应用开发者提供经过充分测试的基础
## 快速开始
以下示例采用 JSON 格式,但配置也可以使用其他[文件格式](https://github.com/node-config/node-config/wiki/Configuration-Files#file-formats)。
**在你的应用目录中安装,并编辑默认配置文件。**
```
$ npm install config
$ mkdir config
$ vi config/default.json
```
```
{
// Customer module configs
"Customer": {
"dbConfig": {
"host": "localhost",
"port": 5984,
"dbName": "customers"
},
"credit": {
"initialLimit": 100,
// Set low for development
"initialDays": 1
}
}
}
```
**编辑用于生产部署的配置覆盖:**
```
$ vi config/production.json
```
```
{
"Customer": {
"dbConfig": {
"host": "prod-db-server"
},
"credit": {
"initialDays": 30
}
}
}
```
**在代码中使用配置:**
```
const config = require('config');
//...
const dbConfig = config.get('Customer.dbConfig');
db.connect(dbConfig, ...);
if (config.has('optionalFeature.detail')) {
const detail = config.get('optionalFeature.detail');
//...
}
```
`config.get()` 会对未定义的键抛出异常,以帮助捕获拼写错误和缺失值。
使用 `config.has()` 来测试某个配置值是否已定义。
**启动你的应用服务器:**
```
$ export NODE_ENV=production
$ node my-app.js
```
在此配置下运行,`dbConfig` 的 `port` 和 `dbName` 元素
将来自 `default.json` 文件,而 `host` 元素将
来自 `production.json` 覆盖文件。
## TypeScript
类型声明发布在 `types/` 下,并通过 `typesVersions` 解析。除了主 `config` 入口外,还包括 `config/parser`、'config/util/defer' 和 `config/lib/util` 的子路径类型。
## 文章
* [配置文件](https://github.com/node-config/node-config/wiki/Configuration-Files)
* [JavaScript 配置文件的特殊功能](https://github.com/node-config/node-config/wiki/Special-features-for-JavaScript-configuration-files)
* [常见用法](https://github.com/node-config/node-config/wiki/Common-Usage)
* [环境变量](https://github.com/node-config/node-config/wiki/Environment-Variables)
* [保留字](https://github.com/node-config/node-config/wiki/Reserved-Words)
* [命令行覆盖](https://github.com/node-config/node-config/wiki/Command-Line-Overrides)
* [多 Node 实例](https://github.com/node-config/node-config/wiki/Multiple-Node-Instances)
* [子模块配置](https://github.com/node-config/node-config/wiki/Sub-Module-Configuration)
* [从数据库/外部源配置](https://github.com/node-config/node-config/wiki/Configuring-from-an-External-Source)
* [保护生产环境配置文件](https://github.com/node-config/node-config/wiki/Securing-Production-Config-Files)
* [外部配置管理工具](https://github.com/node-config/node-config/wiki/External-Configuration-Management-Tools)
* [检查配置来源](https://github.com/node-config/node-config/wiki/Examining-Configuration-Sources)
* [使用配置工具](https://github.com/node-config/node-config/wiki/Using-Config-Utilities)
* [从 Config 0.x 升级](https://github.com/node-config/node-config/wiki/Upgrading-From-Config-0.x)
* [Webpack 用法](https://github.com/node-config/node-config/wiki/Webpack-Usage)
## 更多信息
如果你仍未找到所需内容,可以查看以下更多资源:
* [Wiki](https://github.com/node-config/node-config/wiki)可能有更多未直接在此处链接的页面。
* 在 StackExchange 上查看[标记为 node-config 的问题](https://stackexchange.com/filters/207096/node-config)。这些问题由 `node-config` 贡献者监控。
* [搜索 Issue 追踪器](https://github.com/node-config/node-config/issues)。那里已经讨论并解决了数百个 Issue。
## 贡献者
标签:CMS安全, GNU通用公共许可证, Homebrew安装, HTTP工具, JavaScript, JSON, MITM代理, Node-config, Node.js, npm包, SOC Prime, YAML, 分层配置, 命令行参数, 多环境部署, 安全助手, 安全库, 应用配置, 开发工具, 恶意代码分析, 数据可视化, 环境变量, 网络安全审计, 自定义脚本, 自定义脚本, 软件开发, 配置文件