Swagger UI is not creating on azure but works locally
bariskes11 opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
When I run swagger ui on my localhost everything works perfectly but if I try to deploy my API to azure swagger UI is not creating the UI.
when I write swagger URL like HTTP://{serverUrl}/swagger I get a Not Found exception.
I found some solutions related to that but my problem is not solved. I'm attaching my main.ts (works only locally)
require('dotenv').config();
// import { AppConfigService } from './config/app/config.service';
import { AppModule } from 'src/app.module';
import { NestFactory } from '@nestjs/core';
import { join, resolve } from "path";
import { NestExpressApplication } from '@nestjs/platform-express';
import { get } from 'http';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import * as appPackage from 'package.json';
import * as swaggerUi from 'swagger-ui-express';
import { createWriteStream, writeFileSync } from 'fs';
async function bootstrap() {
const app = await NestFactory.create<NestExpressApplication>(
AppModule,
);
app.enableCors();
app.useStaticAssets(join(__dirname, '..', 'public'));
app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('hbs');
app.set('trust proxy');
// // app.use(json({ limit: '50mb' }));
// // app.use(urlencoded({ extended: true, limit: '50mb' }));
const options = new DocumentBuilder()
.setTitle('api-title')
.setDescription('api description')
.setVersion(appPackage.version)
.addBearerAuth({
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
name: "JWT",
description: "Enter JWT Token",
in: "header"
}, "JWT-auth")
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api-local-doc', app, document, {
swaggerOptions: {
tagsSorter: 'alpha',
operationsSorter: 'method'
}
});
await app.init();
console.log("main is called!");
await app.listen(3000);
}
bootstrap();
and my main.azure.ts I also tried to copy files manually didn't work.
require('dotenv').config();
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { join } from 'path';
import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from './app.module';
import * as appPackage from 'package.json';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import * as swaggerUi from 'swagger-ui-express';
import { createWriteStream, writeFileSync } from 'fs';
import { get } from 'http';
export async function createApp(): Promise<INestApplication> {
const app = await NestFactory.create<NestExpressApplication>(
AppModule,
);
app.enableCors();
app.setGlobalPrefix('api');
app.useStaticAssets(join(__dirname, '..', 'public'));
app.setBaseViewsDir(join(__dirname, '..', 'views'));
app.setViewEngine('hbs');
app.set('trust proxy');
// app.use(json({ limit: '50mb' }));
// app.use(urlencoded({ extended: true, limit: '50mb' }));
const options = new DocumentBuilder()
.setTitle('api-title')
.setDescription('api description')
.setVersion(appPackage.version)
.addBearerAuth({
type: "http",
scheme: "bearer",
bearerFormat: "JWT",
name: "JWT",
description: "Enter JWT Token",
in: "header"
}, "JWT-auth")
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('swagger', app, document, {
swaggerOptions: {
useGlobalPrefix: true,
tagsSorter: 'alpha',
operationsSorter: 'method'
}
});
//writeFileSync("./swagger.json", JSON.stringify(document));
SwaggerModule.setup ('swagger-doc', app, document);
const serverUrl = 'http://mainserver/url'; // Define the route
app.use(serverUrl, swaggerUi.serve, swaggerUi.setup(document));
// I also tried to copy files but it' didn't work
// write swagger ui files
// get(
// `${serverUrl}/swagger/swagger-ui-bundle.js`, function
// (response) {
// response.pipe(createWriteStream('swagger-static/swagger-ui-bundle.js'));
// console.log(
// `Swagger UI bundle file written to: '/swagger-static/swagger-ui-bundle.js'`,
// );
// });
// get(`${serverUrl}/swagger/swagger-ui-init.js`, function (response) {
// response.pipe(createWriteStream('swagger-static/swagger-ui-init.js'));
// console.log(
// `Swagger UI init file written to: '/swagger-static/swagger-ui-init.js'`,
// );
// });
// get(
// `${serverUrl}/swagger/swagger-ui-standalone-preset.js`,
// function (response) {
// response.pipe(
// createWriteStream('swagger-static/swagger-ui-standalone-preset.js'),
// );
// console.log(
// `Swagger UI standalone preset file written to: '/swagger-static/swagger-ui-standalone-preset.js'`,
// );
// });
// get(`${serverUrl}/swagger/swagger-ui.css`, function (response) {
// response.pipe(createWriteStream('swagger-static/swagger-ui.css'));
// console.log(
// `Swagger UI css file written to: '/swagger-static/swagger-ui.css'`,
// );
// });
app.init();
console.log("main is called!");
app.listen(3000);
return app;
}
`
please let me know if you need more details. my node version is 18 lts.
### Minimum reproduction code
https://github.com/bariskes11/quick-review.git
### Steps to reproduce
_No response_
### Expected behavior
the creation of swagger UI on Azure just like at localhost.
### Package version
7.1.10
### NestJS version
10.2.4
### Node.js version
18.17.1
### In which operating systems have you tested?
- [ ] macOS
- [X] Windows
- [ ] Linux
### Other
please let me know if any more details needed
Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.