import { NestFactory } from '@nestjs/core'; import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify'; import { AppModule } from './app.module'; import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; import { HttpExceptionFilter } from './http-exception.filter'; import { ResponseInterceptor } from './response'; async function bootstrap(port: number) { const app = await NestFactory.create( AppModule, new FastifyAdapter({ bodyLimit: 50 * 1048576 // 50MB }) ); // 使用全局异常过滤器 app.useGlobalFilters(new HttpExceptionFilter()); app.useGlobalInterceptors(new ResponseInterceptor()); const config = new DocumentBuilder() .setTitle('Cats example') .setDescription('The cats API description') .setVersion('1.0') .addTag('cats') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('api', app, document); try { await app.listen(port, '0.0.0.0'); console.log(`Application is running on: ${await app.getUrl()}`); } catch (error) { if (error.code === 'EADDRINUSE') { console.warn(`Port ${port} is already in use, trying next port...`); await bootstrap(port + 1); } else { console.error(`Failed to start application: ${error.message}`); process.exit(1); } } } bootstrap(3000);