This article will explain how to integrate swagger (Open API) with Node & express. Swagger makes it very easy for a backend developer to document, test, and explain API endpoints he/she's been working on a front-end developer or anyone looking to consume those endpoints.
Integrate Open API (Swagger) With Node And Express

Setup

Before we get started into this we should have few things installed in our machine.
Source Code - Git Code
Required Packages
npm init
npm install swagger-jsdoc swagger-ui-express express nodemon
Express - For Server
Swagger - For API's Documentation in UI
Nodemon - will use this to restart our server automatically whenever we make changes to our files.
After installing the required packages let's add the new file to set up the Swagger configuration and as well adding the API endpoints in Node
Structure of the Project
Integrate Open API (Swagger) With Node And Express

Setting up the swagger

Swagger UI can be set up for both the front end & backend as well. Since this article is about the Swagger with Node.js. I will be setting up the the swagger in Node.js express app only. you can explore the other options here
In your api.js
  1. //Swagger Configuration
  2. const swaggerOptions = {
  3. swaggerDefinition: {
  4. info: {
  5. title:'Employee API',
  6. version:'1.0.0'
  7. }
  8. },
  9. apis:['api.js'],
  10. }
  11. const swaggerDocs = swaggerJSDoc(swaggerOptions);
  12. app.use('/api-docs',swaggerUI.serve,swaggerUI.setup(swaggerDocs));
After integrating the swagger setup lets define the swagger endpoint with description and response codes in a particular format so that we can able to access those API's inside the swagger after running in the browser
Add Swagger Before Each End Point
  1. /**
  2. * @swagger
  3. * /Employees:
  4. * get:
  5. * description: Get all Employee
  6. * responses:
  7. * 200:
  8. * description: Success
  9. *
  10. */
  11. app.get('/Employees',(req,res)=>{
  12. res.send([
  13. {
  14. id:1, Name:'Jk'
  15. },
  16. {
  17. id:2,Name:'Jay'
  18. }
  19. ])
  20. });
For the demo purpose, I have added four API's (Get, Post, Put, Delete) added the swagger setup for the remaining endpoints as well
Final api.js
  1. const express = require('express');
  2. const swaggerJSDoc = require('swagger-jsdoc');
  3. const swaggerUI = require('swagger-ui-express');
  4. const app = express();
  5. app.listen(5000,()=>console.log("listening on 5000"));
  6. //Swagger Configuration
  7. const swaggerOptions = {
  8. swaggerDefinition: {
  9. info: {
  10. title:'Employee API',
  11. version:'1.0.0'
  12. }
  13. },
  14. apis:['api.js'],
  15. }
  16. const swaggerDocs = swaggerJSDoc(swaggerOptions);
  17. app.use('/api-docs',swaggerUI.serve,swaggerUI.setup(swaggerDocs));
  18. /**
  19. * @swagger
  20. * /Employees:
  21. * get:
  22. * description: Get all Employee
  23. * responses:
  24. * 200:
  25. * description: Success
  26. *
  27. */
  28. app.get('/Employees',(req,res)=>{
  29. res.send([
  30. {
  31. id:1, Name:'Jk'
  32. },
  33. {
  34. id:2,Name:'Jay'
  35. }
  36. ])
  37. });
  38. /**
  39. * @swagger
  40. * /Employees:
  41. * post:
  42. * description: Create an Employee
  43. * parameters:
  44. * - name: EmployeeName
  45. * description: Create an new employee
  46. * in: formData
  47. * required: true
  48. * type: String
  49. * responses:
  50. * 201:
  51. * description: Created
  52. *
  53. */
  54. app.post('/Employees',(req,res)=>{
  55. res.status(201).send();
  56. });
  57. /**
  58. * @swagger
  59. * /Employees:
  60. * put:
  61. * description: Create an Employee
  62. * parameters:
  63. * - name: EmployeeName
  64. * description: Create an new employee
  65. * in: formData
  66. * required: true
  67. * type: String
  68. * responses:
  69. * 201:
  70. * description: Created
  71. *
  72. */
  73. app.put('/Employees',(req,res)=>{
  74. res.status(201).send();
  75. });
  76. /**
  77. * @swagger
  78. * /Employees:
  79. * delete:
  80. * description: Create an Employee
  81. * parameters:
  82. * - name: EmployeeName
  83. * description: Create an new employee
  84. * in: formData
  85. * required: true
  86. * type: String
  87. * responses:
  88. * 201:
  89. * description: Created
  90. *
  91. */
  92. app.delete('/Employees',(req,res)=>{
  93. res.status(201).send();
  94. });
Run the URL in the browser
Now you can do the npm start in the terminal to your app and it will navigate to the browser we have to add the /api-docs at the end of the URL so that i will navigate to the swagger which we have configured and you will see the Swagger UI based on your generated swagger.json.
Terminal
Integrate Open API (Swagger) With Node And Express
Swagger
Integrate Open API (Swagger) With Node And Express
Testing the API with Swagger
Integrate Open API (Swagger) With Node And Express
Hope this article helps you !!
Keep learning ......!