点击Swagger中的“执行”按钮后,浏览器窗口上未显示响应(Nodejs-Express-Mongoose)

ryoqjall  于 2022-11-23  发布在  Go
关注(0)|答案(1)|浏览(601)

我试图点击网址localhost:3000/category/list,这是一个GET请求。不幸的是,我没有在浏览器中得到响应。相同的API已经在Postman中测试过,并得到了正确的结果。我在下面附上swagger配置文件。

const swaggerJsDoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const dotenv = require("dotenv");
dotenv.config({ path: "config/config.env" });

const options = {
  definition: {
    openapi: "3.0.0",
    info: {
      title: "Shopp REST APIs",
      version: "1.0.0",
      description: "API Library for Shopp - Eccomerce App",
    },
    servers: [
      {
        url: `http://localhost:${process.env.PORT}`,
      },
    ],
  },
  apis: ["/home/user/Desktop/eCommerce_App/routes/*.js"],
};
const specs = swaggerJsDoc(options);
module.exports = { specs, swaggerUi };

下面是路由文件:

/**
 * @swagger
 * components:
 *  schemas:
 *      Category:
 *          type: object
 *          properties:
 *              Id:
 *                  type: string
 *              name:
 *                  type: string
 *              slug:
 *                  type: string
 *              parentId:
 *                  type: string
 *
 */

/**
 * @swagger
 * /category/list/:
 *  get:
 *      summmary: Returns the list of categories and subcategories
        responses:
 *          200:
                description: Testing Get
                content:
                    application/json:
                        schema:
                            type: array
                            items:
                                $ref: '#/components/schemas/Category'
 */

router.get("/list", listCategories); // View all the categories

已尝试文档。问题仍然存在

zpgglvta

zpgglvta1#

我猜Swagger UI没有呈现“Responses”部分是因为路由注解中的一些错误导致了无效的API定义。具体来说,您应该在块注解中每行的开头添加*,以便格式和缩进保持一致。
此外,还有一个错字:summmary应该是summary
请尝试按如下方式更改管线标注,看看是否可以解决此问题:

/**
 * @swagger
 * /category/list/:
 *  get:
 *      summary: Returns the list of categories and subcategories
 *      responses:
 *          200:
 *              description: Testing Get
 *              content:
 *                  application/json:
 *                      schema:
 *                          type: array
 *                          items:
 *                              $ref: '#/components/schemas/Category'
 */

相关问题