配置文件使用说明
概述
NestJS Admin 项目的配置文件位于 nest-admin/config/ 目录下,主要包括以下几个文件:
index.ts- 主配置文件secret.ts- 敏感信息配置文件secret.copy.ts- 敏感信息模板文件
配置文件结构
主配置文件 (index.ts)
主配置文件包含了项目的核心配置信息,支持不同环境的配置:
typescript
const env = {
dev: {
// 开发环境配置
},
prod: {
// 生产环境配置
},
}
export const config = {
// 通用配置项
...env[mode], // 根据运行环境选择对应配置
}环境配置
开发环境 (dev)
typescript
{
database: {
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'a1111111',
database: 'nest_admin',
synchronize: true,
autoLoadEntities: true,
logging: true,
}
}生产环境 (prod)
typescript
{
database: {
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'your password',
database: 'nest_admin',
synchronize: true,
autoLoadEntities: true,
}
}配置项详解
数据库配置 (database)
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| type | string | 'mysql' | 数据库类型 |
| host | string | 'localhost' | 数据库主机地址 |
| port | number | 3306 | 数据库端口 |
| username | string | 'root' | 数据库用户名 |
| password | string | - | 数据库密码 |
| database | string | 'nest_admin' | 数据库名称 |
| synchronize | boolean | true | 是否自动同步数据库结构 |
| autoLoadEntities | boolean | true | 是否自动加载实体 |
| logging | boolean | true | 是否开启SQL日志 |
通用配置项
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| apiBase | string | '/api' | API基础路径 |
| adminKey | string | 'admin' | 管理员标识 |
| isPublicKey | string | 'isPublic' | 公共接口标识 |
| jwtExpires | string | 计算值 | JWT过期时间 |
| jwtSecret | string | 复杂字符串 | JWT密钥 |
敏感信息配置
secret.ts 文件
用于存放敏感信息,不应提交到版本控制系统:
typescript
export const secret = {
dev: {
database: {
password: 'your_dev_password',
// 其他敏感配置
}
},
prod: {
database: {
password: 'your_prod_password',
// 其他敏感配置
}
}
}使用方法
- 复制
secret.copy.ts为secret.ts - 修改
secret.ts中的敏感信息 - 确保
secret.ts已添加到.gitignore
环境变量使用
项目通过命令行参数指定运行环境:
bash
# 开发环境
npm run start:dev --env=dev
# 生产环境
npm run start:prod --env=prod配置优先级
配置加载遵循以下优先级(从高到低):
secret.ts中的环境特定配置secret.ts中的通用配置index.ts中的环境配置index.ts中的默认配置
最佳实践
1. 安全配置
typescript
// ✅ 推荐:将敏感信息分离到 secret.ts
// index.ts 中只保留非敏感配置
export const config = {
database: {
type: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
database: 'nest_admin',
synchronize: true,
autoLoadEntities: true,
}
}
// secret.ts 中配置密码等敏感信息
export const secret = {
dev: {
database: {
password: 'secure_password_here'
}
}
}2. 环境隔离
typescript
// ✅ 推荐:为不同环境使用不同的配置
const env = {
dev: {
database: {
host: 'localhost',
logging: true
}
},
prod: {
database: {
host: 'prod-db-server',
logging: false
}
}
}3. 配置验证
typescript
// ✅ 推荐:在应用启动时验证必要配置
async function validateConfig() {
if (!config.database.password) {
throw new Error('Database password is required')
}
if (!config.jwtSecret || config.jwtSecret.includes('DO NOT USE')) {
throw new Error('JWT secret must be configured')
}
}常见问题
Q: 如何添加新的配置项?
A: 在 config 对象中添加新属性,如果涉及敏感信息,应添加到 secret.ts 中。
Q: 如何在代码中使用配置?
A: 通过导入 config 对象使用:
typescript
import { config } from './config'
console.log(config.apiBase)
console.log(config.database.host)Q: 如何处理不同部署环境的配置?
A: 可以扩展 env 对象,添加更多的环境配置:
typescript
const env = {
dev: { /* 开发环境 */ },
test: { /* 测试环境 */ },
staging: { /* 预发布环境 */ },
prod: { /* 生产环境 */ }
}Q: 如何保护敏感配置信息?
A:
- 将敏感信息放在
secret.ts中 - 确保
secret.ts不被提交到版本控制 - 使用环境变量或密钥管理服务
- 定期轮换敏感信息
配置示例
完整的开发环境配置
typescript
// index.ts
export const config = {
apiBase: '/api',
adminKey: 'admin',
isPublicKey: 'isPublic',
jwtSecret: 'your-secure-jwt-secret-key-here',
...env[mode]
}
// secret.ts
export const secret = {
dev: {
database: {
password: 'dev_password_123',
host: 'localhost'
}
}
}生产环境安全配置
typescript
// secret.ts
export const secret = {
prod: {
database: {
password: process.env.DB_PASSWORD,
host: process.env.DB_HOST
},
jwtSecret: process.env.JWT_SECRET
}
}