scottie1984 / swagger-ui-express

Adds middleware to your express app to serve the Swagger UI bound to your Swagger document. This acts as living documentation for your API hosted from within your app.
MIT License
1.43k stars 228 forks source link

Swagger with domain path from AWS API Gateway #301

Open Alex-Kostenko opened 2 years ago

Alex-Kostenko commented 2 years ago

I can't open swagger in the https://domain.com/v1/api. Locally everything works fine. ( http://localhost:4001/api ). After deployment in the testing link, everything works fine also ( https://e4nub5bjh6.execute.us.amazonaws.com/dev/api ). But not want to work on the prod link: https://domain.com/v1/api

Without path v1 everything work ( https://domain.com/api ). image

My code // lambda.ts

const express = require('express');
const pathToSwaggerUi = require('swagger-ui-dist').absolutePath();

const binaryMimeTypes: string[] = [];

function setupSwagger(app: INestApplication) {
  const options = new DocumentBuilder()
    .setTitle(API')
    .setDescription('REST AP')
    .setVersion('1.0.0')
    .addTag('API')
    .build();

  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);
}

async function bootstrapServer(cachedServer: Server): Promise<Server> {
  if (!cachedServer) {
    const expressApp = express();

    expressApp.use(express.static(pathToSwaggerUi));

    const nestApp = await NestFactory.create(
      AppModule,
      new ExpressAdapter(expressApp),
    );
    nestApp.use((req: Request, res: Response, next: NextFunction) => {
      if (req.originalUrl.includes('favicon')) {
        return res.status(204).end();
      }
      next();
    });
    nestApp.enableCors();
    nestApp.setGlobalPrefix('v1');
    nestApp.useGlobalFilters(new AllExceptionFilter());
    nestApp.useGlobalPipes(new ValidationPipe({ transform: true }));
    useContainer(nestApp.select(AppModule), { fallbackOnErrors: true });

    nestApp.use(eventContext());

    setupSwagger(nestApp);

    await nestApp.init();
    cachedServer = createServer(expressApp, undefined, binaryMimeTypes);
  }
  return cachedServer;
}

export const handler: Handler = async (event: any, context: Context) => {
  if (event.path === '/api') {
    event.path = '/api/';
  }

  event.path = event.path.includes('swagger-ui')
    ? '/api${event.path}'
    : event.path;

  let cachedServer: Server;
  cachedServer = await bootstrapServer(cachedServer);
  return proxy(cachedServer, event, context, 'PROMISE').promise;
};

How can I fix this issue?

scottie1984 commented 2 years ago

Can you check the console for the network requests made?