import { NestFactory } from '@nestjs/core'; import { ValidationPipe, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { NestExpressApplication } from '@nestjs/platform-express'; import { join } from 'path'; import { AppModule } from './app.module'; import * as compression from 'compression'; import { HttpExceptionFilter } from './common/filters/http-exception.filter'; async function bootstrap() { const app = await NestFactory.create(AppModule, { logger: ['error', 'warn', 'log', 'debug', 'verbose'], }); // 增加请求体大小限制(支持上传大文件 base64) // 1GB 文件编码后约 1.33GB,加上其他字段,设置为 1500mb app.useBodyParser('json', { limit: '1500mb' }); app.useBodyParser('urlencoded', { limit: '1500mb', extended: true }); const configService = app.get(ConfigService); // 全局验证管道 app.useGlobalPipes( new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true, transformOptions: { enableImplicitConversion: true, }, }), ); // 全局异常过滤器 app.useGlobalFilters(new HttpExceptionFilter()); // 启用压缩 // app.use(compression()); // CORS app.enableCors({ origin: configService.get('FRONTEND_URL') || 'http://localhost:5173', credentials: true, methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS', allowedHeaders: 'Content-Type, Accept, Authorization', }); // 配置静态文件服务(用于访问上传的文件) // 使用绝对路径确保在编译后也能正确找到 uploads 目录 const uploadsPath = join(__dirname, '..', '..', 'uploads'); app.useStaticAssets(uploadsPath, { prefix: '/uploads/', }); // API前缀 app.setGlobalPrefix('api/v1'); const port = configService.get('PORT') || 3000; await app.listen(port); console.log(` ╔═════════════════════════════════════════════════════╗ ║ ║ ║ 🚀 幼儿阅读教学服务平台后端启动成功 ║ ║ ║ ║ 📍 Local: http://localhost:${port} ║ ║ 📍 API: http://localhost:${port}/api/v1 ║ ║ 📍 Prisma: npx prisma studio ║ ║ ║ ╚═════════════════════════════════════════════════════╝ `); } bootstrap();