NodeJS 为什么Swagger不使用完整的路由发出API请求?

mrwjdhj3  于 2023-03-22  发布在  Node.js
关注(0)|答案(1)|浏览(160)

当我使用swagger UI发出请求时,它使用了错误的URL,但是当我在浏览器上发出请求并使用http://localhost:8080/client/getRecord/?id = j 9 jSeEYJOjWWp 6V 5EHP 0时,它工作得很好,因为它有“/client”路由,而不是在Swagger上。

下面是我的index.js代码:

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}));

下面是我的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;

下面是我的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"
          }
        }
      }
    },
  }
}

我通过这个脚本swagger.js使用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')
});

我做错了什么?为什么swagger没有选择完整的/client/getRecord路由?

mwkjh3gx

mwkjh3gx1#

您将endpointsFiles设置为所有路由器文件的列表。Swagger不知道路由器安装在/以外的路径上。
不要传入路由器列表,而是传入index.js文件,其中包含有关路由器安装位置的信息:

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

相关问题