|
| 1 | +--- |
| 2 | +title: 项目配置 |
| 3 | +icon: config |
| 4 | +dir: |
| 5 | + collapsible: false |
| 6 | + expanded: false |
| 7 | + order: 2 |
| 8 | + link: true |
| 9 | +category: |
| 10 | + - 项目配置 |
| 11 | +--- |
| 12 | + |
| 13 | +# 项目配置说明 |
| 14 | + |
| 15 | +本文档介绍 youlai-nest 项目的配置方式和相关参数说明。 |
| 16 | + |
| 17 | +## 配置文件 |
| 18 | + |
| 19 | +youlai-nest 使用多种配置文件管理不同环境的配置: |
| 20 | + |
| 21 | +- `.env`: 默认环境配置 |
| 22 | +- `.env.development`: 开发环境配置 |
| 23 | +- `.env.production`: 生产环境配置 |
| 24 | +- `.env.test`: 测试环境配置 |
| 25 | + |
| 26 | +## 核心配置参数 |
| 27 | + |
| 28 | +### 基础配置 |
| 29 | + |
| 30 | +## 环境变量配置 |
| 31 | + |
| 32 | +youlai-nest 使用 `.env` 文件进行环境配置,主要配置项包括: |
| 33 | + |
| 34 | +### MongoDB 配置 |
| 35 | + |
| 36 | +```env |
| 37 | +# MongoDB连接URI |
| 38 | +MONGODB_URI=mongodb://localhost:27017/youlai |
| 39 | +``` |
| 40 | + |
| 41 | +### Redis 配置 |
| 42 | + |
| 43 | +```env |
| 44 | +# Redis主机地址 |
| 45 | +REDIS_HOST=localhost |
| 46 | +# Redis端口 |
| 47 | +REDIS_PORT=6379 |
| 48 | +# Redis密码(如果有) |
| 49 | +REDIS_PASSWORD= |
| 50 | +``` |
| 51 | + |
| 52 | +### JWT 配置 |
| 53 | + |
| 54 | +```env |
| 55 | +# JWT密钥 |
| 56 | +JWT_SECRET=your-secret-key |
| 57 | +# JWT过期时间 |
| 58 | +JWT_EXPIRES_IN=7d |
| 59 | +``` |
| 60 | + |
| 61 | +### 阿里云 OSS 配置 |
| 62 | + |
| 63 | +```env |
| 64 | +# OSS区域 |
| 65 | +OSS_REGION=oss-cn-hangzhou |
| 66 | +# OSS访问密钥ID |
| 67 | +OSS_ACCESS_KEY_ID=your-access-key |
| 68 | +# OSS访问密钥密码 |
| 69 | +OSS_ACCESS_KEY_SECRET=your-secret-key |
| 70 | +# OSS存储桶名称 |
| 71 | +OSS_BUCKET=your-bucket |
| 72 | +``` |
| 73 | + |
| 74 | +## 应用配置 |
| 75 | + |
| 76 | +通过 NestJS 的 `ConfigModule` 访问配置: |
| 77 | + |
| 78 | +```typescript |
| 79 | +import { ConfigModule, ConfigService } from '@nestjs/config'; |
| 80 | + |
| 81 | +@Module({ |
| 82 | + imports: [ |
| 83 | + ConfigModule.forRoot({ |
| 84 | + isGlobal: true, |
| 85 | + envFilePath: '.env', |
| 86 | + }), |
| 87 | + // 其他模块... |
| 88 | + ], |
| 89 | +}) |
| 90 | +export class AppModule {} |
| 91 | +``` |
| 92 | + |
| 93 | +在服务中使用配置: |
| 94 | + |
| 95 | +```typescript |
| 96 | +import { Injectable } from '@nestjs/common'; |
| 97 | +import { ConfigService } from '@nestjs/config'; |
| 98 | + |
| 99 | +@Injectable() |
| 100 | +export class AppService { |
| 101 | + constructor(private configService: ConfigService) { |
| 102 | + // 获取配置值 |
| 103 | + const mongoUri = this.configService.get<string>('MONGODB_URI'); |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## Swagger API 文档配置 |
| 109 | + |
| 110 | +在 `main.ts` 中配置 Swagger: |
| 111 | + |
| 112 | +```typescript |
| 113 | +import { NestFactory } from '@nestjs/core'; |
| 114 | +import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; |
| 115 | +import { AppModule } from './app.module'; |
| 116 | + |
| 117 | +async function bootstrap() { |
| 118 | + const app = await NestFactory.create(AppModule); |
| 119 | + |
| 120 | + // Swagger文档配置 |
| 121 | + const config = new DocumentBuilder() |
| 122 | + .setTitle('有来后台管理系统API') |
| 123 | + .setDescription('基于NestJS的后台管理系统API文档') |
| 124 | + .setVersion('1.0') |
| 125 | + .addBearerAuth() |
| 126 | + .build(); |
| 127 | + const document = SwaggerModule.createDocument(app, config); |
| 128 | + SwaggerModule.setup('apiDoc', app, document); |
| 129 | + |
| 130 | + await app.listen(8989); |
| 131 | +} |
| 132 | +bootstrap(); |
| 133 | +``` |
| 134 | + |
| 135 | +## 跨域配置 |
| 136 | + |
| 137 | +在 `main.ts` 中启用跨域: |
| 138 | + |
| 139 | +```typescript |
| 140 | +import { NestFactory } from '@nestjs/core'; |
| 141 | +import { AppModule } from './app.module'; |
| 142 | + |
| 143 | +async function bootstrap() { |
| 144 | + const app = await NestFactory.create(AppModule); |
| 145 | + |
| 146 | + // 启用CORS |
| 147 | + app.enableCors(); |
| 148 | + |
| 149 | + await app.listen(8989); |
| 150 | +} |
| 151 | +bootstrap(); |
| 152 | +``` |
| 153 | + |
| 154 | +## 全局管道配置 |
| 155 | + |
| 156 | +添加全局验证管道: |
| 157 | + |
| 158 | +```typescript |
| 159 | +import { NestFactory } from '@nestjs/core'; |
| 160 | +import { ValidationPipe } from '@nestjs/common'; |
| 161 | +import { AppModule } from './app.module'; |
| 162 | + |
| 163 | +async function bootstrap() { |
| 164 | + const app = await NestFactory.create(AppModule); |
| 165 | + |
| 166 | + // 添加全局验证管道 |
| 167 | + app.useGlobalPipes(new ValidationPipe({ |
| 168 | + whitelist: true, |
| 169 | + transform: true, |
| 170 | + })); |
| 171 | + |
| 172 | + await app.listen(8989); |
| 173 | +} |
| 174 | +bootstrap(); |
| 175 | +``` |
| 176 | + |
| 177 | +## 全局前缀配置 |
| 178 | + |
| 179 | +配置API全局前缀: |
| 180 | + |
| 181 | +```typescript |
| 182 | +import { NestFactory } from '@nestjs/core'; |
| 183 | +import { AppModule } from './app.module'; |
| 184 | + |
| 185 | +async function bootstrap() { |
| 186 | + const app = await NestFactory.create(AppModule); |
| 187 | + |
| 188 | + // 设置全局前缀 |
| 189 | + app.setGlobalPrefix('api'); |
| 190 | + |
| 191 | + await app.listen(8989); |
| 192 | +} |
| 193 | +bootstrap(); |
| 194 | +``` |
0 commit comments