【问题标题】:Swagger documentation not working for Express serverSwagger 文档不适用于 Express 服务器
【发布时间】:2021-08-25 01:10:33
【问题描述】:

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

docs.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!。这是 routes 文件夹中的文件之一。

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。但是在docs.ts 的apis 数组中,我用*.routes.ts 指定文件。我是否必须将其更改为*routes.js

谢谢!

【问题讨论】:

  • 对于您的附带问题,我很确定您不必更改路径。执行或编译时会自动解析

标签: javascript typescript express swagger jsdoc


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-04-25
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 2017-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多