swagger code fixed for server url
This commit is contained in:
21
src/main.ts
21
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}`);
|
||||
|
||||
Reference in New Issue
Block a user