Adding Swagger
nclsmitchell opened this issue · 8 comments
Hi,
I would like to add a Swagger to my Nest application using the following method: https://docs.nestjs.com/recipes/swagger but I can't make it work with the serverless version of index.ts
Here is my actual code:
import { Context, Handler } from 'aws-lambda';
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
import { Server } from 'http';
import * as serverless from 'aws-serverless-express';
import * as express from 'express';
const expressApp = express();
let cachedServer: Server;
function bootstrapServer(): Promise<Server> {
return NestFactory.create(AppModule, expressApp)
.then(app => app.enableCors())
.then(app => app.init())
.then(app => {
const options = new DocumentBuilder()
.setTitle('Hellocase API')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, options);
return SwaggerModule.setup('/swagger', app, document);
})
.then(() => serverless.createServer(expressApp));
}
export const handler: Handler = (event: any, context: Context) => {
if (!cachedServer) {
bootstrapServer().then(server => {
cachedServer = server;
return serverless.proxy(server, event, context);
});
} else {
return serverless.proxy(cachedServer, event, context);
}
};
Do you know how I could make it work?
Thank you in advance!
@rdlabo ?
Hi, same problem. Is there any clue or solution on developer side?
Thank you!
Try app.init() after the setup, that seems to work.
I also had the same problem. Solved by adding the another bootstrap script for swagger only (not the best approach, but it works). So the serverless main handler is still the main.ts
, but when you want to using the swagger, run the swagger bootstrap script.
- src
| - main.ts
| - swagger.ts
...
Run swagger server
ts-node src/swagger.ts
swagger.ts code example from the https://docs.nestjs.com/recipes/swagger
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ApplicationModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(ApplicationModule);
const options = new DocumentBuilder()
.setTitle('Cats example')
.setDescription('The cats API description')
.setVersion('1.0')
.addTag('cats')
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api', app, document);
await app.listen(3001);
}
bootstrap();
I update this repository for nestjs6. And added swagger.ts, and write README.
@ansoncen Thanks. It was helpful.
How can I use Swagger with same port as the index.ts ( Serverless Lambda ) ?
I am getting error :
[Nest] 13722 - 2019-07-12 10:17 PM [PackageLoader] The "swagger-ui-express" package is missing. Please, make sure to install this library ($ npm install swagger-ui-express) to take advantage of SwaggerModule. +8ms
I am 100% sure I have 'swagger-ui-express' in my project.
function bootstrapServer(): Promise {
let that = this;
const expressApp = express();
const adapter = new ExpressAdapter(expressApp);
return NestFactory.create(AppModule, adapter)
.then(app => app.enableCors())
.then((app) => {
const swaggerOptions = new DocumentBuilder()
.setBasePath('/api/')
.setTitle('Mail Services API')
.setDescription('API desciptions')
.setVersion('1.0')
.build();
const swaggerAPIDocument = SwaggerModule.createDocument(app, swaggerOptions);
SwaggerModule.setup('swagger', app, swaggerAPIDocument);
return app;
})
.then(app => app.init())
.then((app) => {
return serverless.createServer(expressApp)
});
}
This is mine index.ts
Take a look at nestjs/swagger#199,
It saved my time when deploy netjs app with swagger