From 383c32db7c14dc526aaea4e43d823d1092be2a9b Mon Sep 17 00:00:00 2001 From: Mayank Mishra Date: Mon, 27 Oct 2025 12:37:39 +0530 Subject: [PATCH] swagger code fixed for server url --- src/main.ts | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index 0d7e37b..53f3029 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,6 +4,8 @@ import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; import { ConfigService } from '@nestjs/config'; import helmet from 'helmet'; import { AppModule } from './app.module'; +import { writeFileSync } from 'fs'; +import { join } from 'path'; async function bootstrap() { const app = await NestFactory.create(AppModule); @@ -12,12 +14,17 @@ async function bootstrap() { // Security app.use(helmet()); app.enableCors({ - origin: configService.get('CORS_ORIGIN', 'http://localhost:3000'), + origin: [ + 'http://localhost:3000', // local Swagger UI + 'https://editor.swagger.io', // Swagger Editor online + ], + methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS', credentials: true, }); - // Global prefix - app.setGlobalPrefix(configService.get('API_PREFIX', 'api/v1')); + // ✅ Global prefix + const globalPrefix = configService.get('API_PREFIX', 'api/v1'); + app.setGlobalPrefix(globalPrefix); // Request logging middleware app.use((req, res, next) => { @@ -36,6 +43,7 @@ async function bootstrap() { }, }), ); + const port = configService.get('PORT', 3000); // Swagger documentation (only in development) if (configService.get('NODE_ENV') !== 'production') { @@ -44,13 +52,18 @@ async function bootstrap() { .setDescription('Professional NestJS serverless application with user CRUD and authentication') .setVersion(configService.get('API_VERSION', '1.0.0')) .addBearerAuth() + .addServer(`${process.env.SERVER_URL}/`, 'Local Server') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('api/docs', app, document); + + const outputPath = join(process.cwd(), 'swagger.json'); + writeFileSync(outputPath, JSON.stringify(document, null, 2), 'utf8'); + console.log(`✅ Swagger JSON file generated at: ${outputPath}`); } - const port = configService.get('PORT', 3000); + // const port = configService.get('PORT', 3000); await app.listen(port); console.log(`🚀 Application is running on: http://localhost:${port}`);