Search code examples
node.jsexpressswagger

Why isn't Swagger making API requests with the full route?


When I Make a request using the swagger UI, it uses the wrong URL, but when I make the request on the browser and use http://localhost:8080/client/getRecord/?id=j9jSeEYJOjWWp6V5EHP0, it works just fine, since it has the "/client" route, and not on Swagger.

enter image description here

For context, here is my index.js code:

const express = require('express');
const app = express();
const cors=require('cors');
const bodyParser = require('body-parser');
const config = require("./config");
const swaggerUi = require('swagger-ui-express')
const swaggerFile = require('./swagger/swagger_output.json')

// Definição de rotas
const authRoutes = require('./routes/auth-routes');
const arduinoRoutes = require('./routes/arduino-routes');

const clientRoutes = require('./routes/client-routes');

app.use('/arduino', arduinoRoutes);
app.use('/auth', authRoutes);
app.use('/client', clientRoutes);

app.listen(config.port, () => console.log('App is listening on url ' + config.url));

app.use('/doc', swaggerUi.serve, swaggerUi.setup(swaggerFile, {explorer:true}));

Here is my client-routes.js:

const express = require('express');
const firebase = require('../db');
const Student = require('../models/record');
const firestore = firebase.firestore();

const router = express.Router();

router.get('/getRecord', (req, res) => {
    // #swagger.tags = ['Client']
    // #swagger.summary = "Get a single record from DB through its Id"
    try {
        recordId = req.query.id;

        firestore.collection('ToDoList').doc(recordId).get().then(function (doc) {
            console.log(doc.data().humidade);
        });

        res.status(200).send(doc.data())
    } catch (error) {
        res.status(400).send(error.message);
    }
});
module.exports = router;

Here is my swagger_output.json:

{
  "swagger": "2.0",
  "info": {
    "version": "1.0.0",
  },
  "host": "localhost:8080",
  "basePath": "/",
  "schemes": [
    "http"
  ],
  "paths": {
    "/getRecord": {
      "get": {
        "tags": [
          "Client"
        ],
        "summary": "Get a single record from DB through its Id",
        "description": "",
        "parameters": [
          {
            "name": "id",
            "in": "query",
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          },
          "400": {
            "description": "Bad Request"
          }
        }
      }
    },
  }
}

Which I created through this script swagger.js, using swagger-autogen:

const swaggerAutogen = require('swagger-autogen')()

const outputFile = './swagger/swagger_output.json'
const endpointsFiles = ['./routes/arduino-routes.js', './routes/auth-routes.js', './routes/client-routes.js']

const doc = {
    info: {
        version: "1.0.0",
    },
    "host": "localhost:8080",
}

swaggerAutogen(outputFile, endpointsFiles, doc).then(() => {
    require('../index.js')
});

What am I doing wrong? Why isn't swagger picking up the full /client/getRecord route?


Solution

  • You're setting endpointsFiles to a list of all your router files. Swagger doesn't know that the routers are mounted at paths other than /.

    Instead of passing in a list of your routers, pass in the index.js file, which contains the information as to where the routers are mounted:

    const endpointsFiles = ['./index.js'];