Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Swagger #1

Closed
nclsmitchell opened this issue Jan 19, 2019 · 8 comments
Closed

Adding Swagger #1

nclsmitchell opened this issue Jan 19, 2019 · 8 comments

Comments

@nclsmitchell
Copy link

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!

@nclsmitchell
Copy link
Author

@rdlabo ?

@csakbalint
Copy link

csakbalint commented Mar 5, 2019

Hi, same problem. Is there any clue or solution on developer side?
Thank you!

@mattds
Copy link

mattds commented Mar 9, 2019

Try app.init() after the setup, that seems to work.

@ansoncen
Copy link

ansoncen commented Mar 19, 2019

@csakbalint @nclsmitchell

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();

@rdlabo
Copy link
Collaborator

rdlabo commented Mar 24, 2019

I update this repository for nestjs6. And added swagger.ts, and write README.

@ansoncen Thanks. It was helpful.

@rdlabo rdlabo closed this as completed Mar 24, 2019
@karocksjoelee
Copy link

karocksjoelee commented Jul 12, 2019

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.

@karocksjoelee
Copy link

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

@tuna318
Copy link

tuna318 commented Apr 24, 2020

Take a look at nestjs/swagger#199,
It saved my time when deploy netjs app with swagger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants