Skip to content

Instantly share code, notes, and snippets.

@santoshshinde2012
Last active October 17, 2022 17:23
Show Gist options
  • Save santoshshinde2012/7acef2934d0e1c458b7dc48379016f24 to your computer and use it in GitHub Desktop.
Save santoshshinde2012/7acef2934d0e1c458b7dc48379016f24 to your computer and use it in GitHub Desktop.
Setup Swagger API Documentation in Node js with Express js and Typescript

Step1 - Install below npm modules with in our application

npm install --save-dev swagger-ui-express @types/swagger-ui-express

Step2 - Create swagger.json file in root directory with below config

{
    "swagger": "2.0",
    "info": {
        "description": "Node Typescript Boilerplate for Microservices. Skeleton for Node.js Apps written in TypeScript (with Setup Instructions for ESLint, Prettier, and Husky)",
        "version": "1.0.0",
        "title": "Node Boilerplate"
    },
    "host": "",
    "basePath": "/api",
    "tags": [{
        "name": "node-boilerplate",
        "description": "Node Typescript Boilerplate"
    }],
    "schemes": ["http", "https"],
    "paths": {
        "/status/system": {
            "get": {
                "tags": ["node-boilerplate"],
                "summary": "get system status",
                "description": "",
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "schema": {
                            "$ref": "#/definitions/ApiResponse"
                        }
                    }
                },
                "security": []
            }
        },
        "/status/time": {
            "get": {
                "tags": ["node-boilerplate"],
                "summary": "get server time",
                "description": "",
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "schema": {
                            "$ref": "#/definitions/ApiResponse"
                        }
                    }
                },
                "security": []
            }
        },
        "/status/usage": {
            "get": {
                "tags": ["node-boilerplate"],
                "summary": "get server usage",
                "description": "",
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "schema": {
                            "$ref": "#/definitions/ApiResponse"
                        }
                    }
                },
                "security": []
            }
        },
        "/status/process": {
            "get": {
                "tags": ["node-boilerplate"],
                "summary": "get server process",
                "description": "",
                "responses": {
                    "200": {
                        "description": "successful operation",
                        "schema": {
                            "$ref": "#/definitions/ApiResponse"
                        }
                    }
                },
                "security": []
            }
        }
    },
    "securityDefinitions": {},
    "definitions": {
        "ApiResponse": {
            "type": "object",
            "properties": {
            }
        }
    },
    "externalDocs": {
        "description": "Find out more about Node Typescript Boilerplate",
        "url": "https://blog.santoshshinde.com/skeleton-for-node-js-apps-written-in-typescript-444fa1695b30"
    }
}

Step3 - Update tsconfig.json to load/import json file

 "resolveJsonModule": true

Step4 - Import swagger UI module and swagger json config file in src/App.ts

import swaggerUi from 'swagger-ui-express';
import swaggerDocument from '../swagger.json'

Step5 - Setup Swagger doc with express route

 this.express.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

Step6 - Browser Swagger API Documentation

The swagger documentation is available at the following url ${host}/docs:

http://localhost:3146/docs

Example - Please check below repository for Node Typescript Boilerplate written in TypeScript

https://github.com/santoshshinde2012/node-boilerplate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment