Swagger Documentation
1. Open framework.
2. Used for designing, documenting, and testing API
3. User-friendly interface for the developers to interact with the API
4. In Postman few things need to be entered manually, but here it's not.
5. Reference URL -> https://swagger.io/
6. Swagger Codegen, Swagger Editor, and Swagger UI are open-source tools.
Swagger Codegen - Generate server stubs and client SDKs from OpenAPI Specification
definitions.
Swagger Editor – API editor for designing APIS with the OpenAPI and AsyncAPI specifications.
Swagger UI – Visualize OpenAPI specification definitions in an interactive UI.
1. npm install swagger-jsdoc@6.0.0 -- save
it will be saved in package.json also.
2. npm install swagger-ui-express --save
3. npm install swagger-autogen -- save
Create the definitions so we need to import below files in mongoodb.js,
Const swaggerJSDoc = require (‘swagger-jsdoc’)
Const swagger/ui = require (‘swagger-ui-express’)
Const options ={
Definition: {
Openapi: ‘3.0.0’,
Info : {
title : ‘node js api project for mongodb’,
Version: ‘1.0.0’
},
Servers:[
{
uri: ‘http://localhost:8000/’
}
]
},
apis :[ ‘./mongodb.js’ ] i.e, ‘ list of api listed filename need to be given’
}
const swaggerSpec = swaggerJSDoc(options)
app.use( ‘/api-docs’, swagerUi.serve, swaggerUi.setup(swaggerSpec))