Other features

This page lists all the other available features that you may find useful.

Global prefix

To ignore a global prefix for routes set through setGlobalPrefix(), use ignoreGlobalPrefix:

  1. const document = SwaggerModule.createDocument(app, options, {
  2. ignoreGlobalPrefix: true,
  3. });

Multiple specifications

The SwaggerModule provides a way to support multiple specifications. In other words, you can serve different documentation, with different UIs, on different endpoints.

To support multiple specifications, your application must be written with a modular approach. The createDocument() method takes a 3rd argument, extraOptions, which is an object with a property named include. The include property takes a value which is an array of modules.

You can setup multiple specifications support as shown below:

  1. import { NestFactory } from '@nestjs/core';
  2. import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
  3. import { AppModule } from './app.module';
  4. import { CatsModule } from './cats/cats.module';
  5. import { DogsModule } from './dogs/dogs.module';
  6. async function bootstrap() {
  7. const app = await NestFactory.create(AppModule);
  8. /**
  9. * createDocument(application, configurationOptions, extraOptions);
  10. *
  11. * createDocument method takes an optional 3rd argument "extraOptions"
  12. * which is an object with "include" property where you can pass an Array
  13. * of Modules that you want to include in that Swagger Specification
  14. * E.g: CatsModule and DogsModule will have two separate Swagger Specifications which
  15. * will be exposed on two different SwaggerUI with two different endpoints.
  16. */
  17. const options = new DocumentBuilder()
  18. .setTitle('Cats example')
  19. .setDescription('The cats API description')
  20. .setVersion('1.0')
  21. .addTag('cats')
  22. .build();
  23. const catDocument = SwaggerModule.createDocument(app, options, {
  24. include: [CatsModule],
  25. });
  26. SwaggerModule.setup('api/cats', app, catDocument);
  27. const secondOptions = new DocumentBuilder()
  28. .setTitle('Dogs example')
  29. .setDescription('The dogs API description')
  30. .setVersion('1.0')
  31. .addTag('dogs')
  32. .build();
  33. const dogDocument = SwaggerModule.createDocument(app, secondOptions, {
  34. include: [DogsModule],
  35. });
  36. SwaggerModule.setup('api/dogs', app, dogDocument);
  37. await app.listen(3000);
  38. }
  39. bootstrap();

Now you can start your server with the following command:

  1. $ npm run start

Navigate to http://localhost:3000/api/cats to see the Swagger UI for cats:

Other features - 图1

In turn, http://localhost:3000/api/dogs will expose the Swagger UI for dogs:

Other features - 图2