Quick Start
This section shows a quick way to implement swagger-autogen in your project. The code below must be inserted in a separate file (e.g swagger.js):
- CommonJs
- ES Modules
swagger.js
const swaggerAutogen = require('swagger-autogen')();
const doc = {
info: {
title: 'My API',
description: 'Description'
},
host: 'localhost:3000'
};
const outputFile = './swagger-output.json';
const routes = ['./path/userRoutes.js', './path/bookRoutes.js'];
/* NOTE: If you are using the express Router, you must pass in the 'routes' only the
root file where the route starts, such as index.js, app.js, routes.js, etc ... */
swaggerAutogen(outputFile, routes, doc);
swagger.js
import swaggerAutogen from 'swagger-autogen';
const doc = {
info: {
title: 'My API',
description: 'Description'
},
host: 'localhost:3000'
};
const outputFile = './swagger-output.json';
const routes = ['./path/userRoutes.js', './path/bookRoutes.js'];
/* NOTE: If you are using the express Router, you must pass in the 'routes' only the
root file where the route starts, such as index.js, app.js, routes.js, etc ... */
swaggerAutogen()(outputFile, routes, doc);
To build the documentation, add the following script in your project's package.json file:
package.json
...
"scripts": {
...
"swagger": "node ./swagger.js"
}
And run the following command:
npm run swagger
Examples
Links to projects that cover the simplest use and the most complete use of this module. See the links below: