1

Hello I am creating API doc for my node API using swagger but my screen doesn't show any end point my code is as below:

in app.js:

const swaggerJsonDoc = require('swagger-jsdoc')
const swaggerUI = require('swagger-ui-express')
const apiDoc = require('./swagger-doc/api-doc.json')


const swaggerOptions = {
    swaggerDefinition: {
        info: {
            title: "API's Document",
            version: "1.0.0.1",
            description: "This document will walk you through all the API end points in swagger",
        },
        servers: [{
            url: "http://localhost:3000"
        }]
    },
    apis: ['./swagger-doc/api-doc.json']
}

const swaggerDocs = swaggerJsonDoc(swaggerOptions)

app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(swaggerDocs))

And my api-doc.json is as below:

{
"swagger": "2.0",
    "host": "localhost:3000",
    "basePath": "/api",
    "schemes": [
      "http"
    ],
    "consumes": [
      "application/json"
    ],
    "produces": [
      "application/json"
    ],
    "paths": {
      "/login": {
        "post": {
          "summary": "Login",
          "tags": [
            "Misc"
          ],
          "operationId": "Login",
          "deprecated": false,
          "produces": [
            "application/json"
          ],
          "parameters": [
            {
              "name": "Body",
              "in": "body",
              "required": true,
              "description": "",
              "schema": {
                "$ref": "#/definitions/LoginRequest"
              }
            }
          ],
          "responses": {
            "200": {
              "description": "",
              "headers": {}
            }
          }
        }
      }
    },
    "definitions": {
      "LoginRequest": {
        "title": "LoginRequest",
        "example": {
          "Email": "abc@xyz.com",
          "Password": "abc@123"
        },
        "type": "object",
        "properties": {
          "Email": {
            "type": "string"
          },
          "Password": {
            "type": "string"
          }
        },
        "required": [
          "Email",
          "Password"
        ]
      }
    },
    "tags": [
      {
        "name": "Misc",
        "description": ""
      }
    ]
  }

I am getting output as : blank with Swagger info only with

No operations defined in spec!

Please help me to find out what exactly I am missing here or should I process with something else.

DevError404
  • 129
  • 2
  • 14
  • Your `api-doc.json` file is missing `"swagger": "2.0"`. – Helen Aug 02 '21 at 20:18
  • thanks for the notice but after including that still have the same issue. updating code for reference – DevError404 Aug 02 '21 at 20:32
  • 1
    You seem to have a mixup of [swagger-ui-express](https://github.com/scottie1984/swagger-ui-express#readme) and [swagger-jsdoc](https://github.com/Surnet/swagger-jsdoc#readme) configs, which while looking similar are somewhat different. For example, swagger-jsdoc's `apis: ['./src/routes*.js']` line is supposed to point to annotated controller files rather than an entire OpenAPI file (api-doc.json); swagger-jsdoc then generates an OpenAPI JSON file based on controlled annotations. – Helen Aug 02 '21 at 20:44
  • 1
    If you have a ready OpenAPI JSON file that you need to display, you don't need `swagger-jsdoc` - see the example in [swagger-ui-express README](https://github.com/scottie1984/swagger-ui-express#usage). – Helen Aug 02 '21 at 20:46
  • Thankyou so much. I removed the swagger-jsdoc and go with swagger-ui-express. I am able to create the doc properly. – DevError404 Aug 03 '21 at 04:29

0 Answers0