首页 > 解决方案 > Swagger 文档不适用于 Express 服务器

问题描述

所以基本上我有一个Express我一直在研究的 API,最近我有了为它实现文档的想法。我选择 Swagger 和 JSDoc 来执行此操作,因为它既快速又简单。但是文档不起作用。到目前为止,这是我实现它的方式。

文档.ts:

import swaggerJSDoc, { Options } from "swagger-jsdoc";

const swaggerOptions: Options = {
   definition: {
      info: {
         title: "Project Nonya Documentation.",
         description: "Documentation for the Project Nonya API",
         contact: {
            name: "Joe Mama",
         },
         version: "6.9.0",
      },
   },
   apis: ["./routes/*.routes.ts"],
};

export const swaggerDocs = swaggerJSDoc(swaggerOptions);

路由器.ts:

...

import swaggerUi from "swagger-ui-express";
import { swaggerDocs } from "./docs";

...

app.use("/api/documentation", swaggerUi.serve, swaggerUi.setup(swaggerDocs));

...

当我到达 docs 端点时,文档确实出现了,但是它一直说No Operations defined in spec!. 这是路线文件夹中的文件之一。

testing.routes.ts:

import express, { Router } from "express";
import { testing} from "../controllers/testing/testing.controller";

const router: Router = express.Router();

/**
 * @swagger
 * /:
 *   get:
 *     description: Why you no work?
 *     responses:
 *       200:
 *         description: Returns nothing cause this shit won't work.
 */
router.post("/testing/:id", testing);

export default router;

我究竟做错了什么?我想不通。目录如下:src/docs.tssrc/router.tssrc/routes/testing.router.ts

附带问题

由于我使用的是 TypeScript,我显然必须将代码编译成 JavaScript。但是在 apis 数组中docs.ts,我用*.routes.ts. 我是否必须将其更改为*routes.js.

谢谢你!

标签: javascripttypescriptexpressswaggerjsdoc

解决方案


发现这个 Github 问题可以解决我的问题。

https://github.com/Surnet/swagger-jsdoc/issues/150


推荐阅读