[Node] Swagger

·

1 min read

[Node] Swagger

How to Use Swagger Library

Swagger: library for API specification document

  1. Install the package
npm install swagger-ui-express swagger-autogen
  1. change app.js
const swaggerUi = require("swagger-ui-express");
const swaggerFile = require("./swagger-output");


app.use("/swagger", swaggerUi.serve, swaggerUi.setup(swaggerFile));
  1. 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);
  1. run command
node ./swagger.js
  1. 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.