How to add Swagger UI to an existing Node.js and Express.js project

How to add Swagger UI to an existing Node.js and Express.js project

npm install

npm run start

open browser and run below url

http://localhost:8000/

npm i swagger-ui-express -S


"swagger": "2.0",
"info": {
"version": "1.0.0", //version of the OpenAPI Specification
"title": "My User Project CRUD",
"description": "My User Project Application API",
"license": {
"name": "MIT",
"url": "
}
},
"host": "localhost:8000",
"basePath": "/",
"tags": [
{
"name": "Users",
"description": "API for users in the system"
}
],
"schemes": ["http"],
"consumes": ["application/json"],
"produces": ["application/json"]
}

"swagger": "2.0","info": {"version": "1.0.0", //version of the OpenAPI Specification"title": "My User Project CRUD","description": "My User Project Application API","license": {"name": "MIT","url": " https://opensource.org/licenses/MIT },"host": "localhost:8000","basePath": "/","tags": ["name": "Users","description": "API for users in the system"],"schemes": ["http"],"consumes": ["application/json"],"produces": ["application/json"]

const swaggerUi = require(‘swagger-ui-express’),

swaggerDocument = require(‘./swagger.json’);

app.use(
'/api-docs',
swaggerUi.serve,
swaggerUi.setup(swaggerDocument)
);

So we will add Path and definition to our swagger.json file. if you are not familiar with paths and it’s the definition then please refer to this link for more details.

{
"paths": {
"/users": {
"get": {
"tags": ["Users"],
"summary": "Get all users in system",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Users"
}
}
}
}
}
},
"definitions": {
"User": {
"required": ["name", "_id", "companies"],
"properties": {
"_id": {
"type": "integer",
"uniqueItems": true
},
"isPublic": {
"type": "boolean"
},
"name": {
"type": "string"
},
"books": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"amount": {
"type": "number"
}
}
}
},
"companies": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"Users": {
"type": "array",
"$ref": "#/definitions/User"
}
}
}

{
"paths": {
"/users": {
"get": {
"tags": ["Users"],
"summary": "Get all users in system",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Users"
}
}
}
}
},
"/addUser": {
"post": {
"tags": ["Users"],
"description": "Create new user in system",
"parameters": [
{
"name": "user",
"in": "body",
"description": "User that we want to create",
"schema": {
"$ref": "#/definitions/User"
}
}
],
"produces": ["application/json"],
"responses": {
"200": {
"description": "New user is created",
"schema": {
"$ref": "#/definitions/User"
}
}
}
}
}
}
}

"/user/{id}": {
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"description": "ID of user that we want to find",
"type": "integer"
}
],
"put": {
"summary": "Update user with give ID",
"tags": ["Users"],
"parameters": [
{
"name": "user",
"in": "body",
"description": "User with new values of properties",
"schema": {
"$ref": "#/definitions/updateUser"
}
}
],
"responses": {
"200": {
"description": "User is updated",
"schema": {
"$ref": "#/definitions/User"
}
}
}
}
}

"updateUser": {
"required": ["name", "companies"],
"properties": {
"isPublic": {
"type": "boolean"
},
"name": {
"type": "string"
},
"books": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"amount": {
"type": "number"
}
}
}
},
"companies": {
"type": "array",
"items": {
"type": "string"
}
}
}
}

"delete": {
"summary": "Delete user with given ID",
"tags": ["Users"],
"responses": {
"200": {
"description": "User is deleted",
"schema": {
"$ref": "#/definitions/User"
}
}
}
}

Conclusion: