node.jsexpressswaggerswagger-uiswagger-jsdocs

Why swagger not direct to the url i specified in yaml file?


I am trying to create a swagger API documentation in express and node . This is my part of code i have done .

index.js file

const express = require('express');
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const http = require('http')

const app = express();
http.createServer(app).listen(3001)
console.log("Listening at:// port:%s (HTTP)", 3001)

const swaggerDefinition = {
    info: {
      title: 'REST API for my App', // Title of the documentation
      version: '1.0.0', // Version of the app
      description: 'This is the REST API for my product', // short description of the app
    },
  };

  const options = {
    // import swaggerDefinitions
    swaggerDefinition,
    // path to the API docs
    apis: ['./docs/*.yaml'],
  };
  // initialize swagger-jsdoc
  const swaggerSpec = swaggerJSDoc(options);
  
  // use swagger-Ui-express for your app documentation endpoint
  app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

my .yaml file

host: localhost:3016
basePath: "/productRule"
schemes:
- http
consumes: []
produces: []
paths:
  "/search":
    post:
      tags:               # Tag property
        - search            # Value of the tag
      summary: search
      produces:
      - application/json
      parameters:         # request parameters
      - in: body          # request body
        name: search
      
            # name of request, can be any name
        description: It enables a user to create an account
        required: false   # can also be true depending on user preference
        schema:           # Schema definition
          $ref: '#/definitions/search' 
      responses:          # server responses
        200:
          description: An object with user details
definitions:        # Schema defination for request body
  search:
    type: object
    properties:
      search:
        type: object
        properties:
          pageSize:
            type: string
          sortBy:
            type: string
          searchQuery:
            type: string

Here i want to call an api that is running in port 3016. But when i am running this script the swagger calling port 3001 , where i defined the swagger script as shown in the screenshot .

enter image description here

Can you please help me to find where I gone wrong?


Solution

  • Base path and URL should define within the swagger definition, Else the default port with base path \ will be taken as api path .

    const swaggerDefinition = {
        info: {
          title: 'REST API for my App', // Title of the documentation
          version: '1.0.0', // Version of the app
          description: 'This is the REST API for my product', // short description of the app
        },
       host: localhost:3016
       basePath: "/productRule"
      };