kindergarten/reading-platform-backend/src/main.ts
tonytech 7f757b6a63 初始提交:幼儿园阅读平台三端代码
- reading-platform-backend:NestJS 后端
- reading-platform-frontend:Vue3 前端
- reading-platform-java:Spring Boot 服务端
2026-02-28 17:51:15 +08:00

75 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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<NestExpressApplication>(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<number>('PORT') || 3000;
await app.listen(port);
console.log(`
╔═════════════════════════════════════════════════════╗
║ ║
║ 🚀 幼儿阅读教学服务平台后端启动成功 ║
║ ║
║ 📍 Local: http://localhost:${port}
║ 📍 API: http://localhost:${port}/api/v1 ║
║ 📍 Prisma: npx prisma studio ║
║ ║
╚═════════════════════════════════════════════════════╝
`);
}
bootstrap();