Skip to content

Instantly share code, notes, and snippets.

@vivekyadav5750
Last active January 23, 2024 05:59
Show Gist options
  • Save vivekyadav5750/d386e947de45299122b959c13530698e to your computer and use it in GitHub Desktop.
Save vivekyadav5750/d386e947de45299122b959c13530698e to your computer and use it in GitHub Desktop.
Nodejs Swagger

Node.js Swagger 3.0 Integration

index.js

// index.js

import swagger from 'swagger-ui-express';
import apiDocs from './swagger.json'; // Import swagger.json
import { assert } from 'swagger-ui-express'; // Import assert for Swagger 3.0 compatibility

// Use swagger.json for Swagger UI
server.use("/api-docs", swagger.serve, swagger.setup(apiDocs));

swagger.json

{
    "openapi": "3.0.0",
    "info": {
        "version": "1.0.0",
        "description": "API for E-Commerce application",
        "title": "E-commerce API"
    },
    "servers": [
        {
            "url": "http://localhost:3200"
        }
    ],
    "components": {
        "securitySchemes": {
            "JWT": {
                "in": "header",
                "name": "Authorization",
                "type": "apiKey"
            }
        }
    },
    "paths": {
        "/api/products": {
            "get": {
                "tags": ["Products"],
                "summary": "Get Products",
                "description": "User will get all products",
                "security": [{ "JWT": {} }],
                "responses": {
                    "200": {
                        "description": "OK"
                    },
                    "401": {
                        "description": "Unauthorized"
                    }
                }
            }
        },
        "/api/users/signin": {
            "post": {
                "tags": ["Users"],
                "summary": "Login",
                "description": "User login to get token",
                "requestBody": {
                    "content": {
                        "application/json": {
                            "schema": {
                                "type": "object",
                                "properties": {
                                    "email": {
                                        "type": "string"
                                    },
                                    "password": {
                                        "type": "string"
                                    }
                                }
                            }
                        }
                    }
                },
                "responses": {
                    "200": {
                        "description": "OK"
                    },
                    "400": {
                        "description": "Incorrect Credentials"
                    }
                }
            }
        }
    }
}

In this example, the swagger.json file defines an OpenAPI 3.0 specification for an E-Commerce API. The index.js file imports and serves this specification using Swagger UI. Adjust the paths, tags, and other details in swagger.json to match your API's structure and documentation needs.

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