How to Use Swagger Library
Swagger: library for API specification document
- Install the package
npm install swagger-ui-express swagger-autogen
- change
app.js
const swaggerUi = require("swagger-ui-express");
const swaggerFile = require("./swagger-output");
app.use("/swagger", swaggerUi.serve, swaggerUi.setup(swaggerFile));
- add
swagger.js
file in the root directory and insert the following code
const swaggerAutogen = require("swagger-autogen")();
const doc = {
info: {
title: "My API",
description: "Description",
},
host: "localhost:3000",
schemes: ["http"],
};
const outputFile = "./swagger-output.json";
const endpointsFiles = [
"./app.js"
];
swaggerAutogen(outputFile, endpointsFiles, doc);
- run command
node ./swagger.js
- Change
swagger-output.json
file for details and use the command above every time you modify the swagger-output.json
file
- After you made changes in API routes, you should run the command above to reflect the changes, but
swagger-output.json
file will be initialized, not saving your additional description.