javascript - 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.ts
、src/router.ts
和src/routes/testing.router.ts
。
附带问题
由于我使用的是 TypeScript,我显然必须将代码编译成 JavaScript。但是在 apis 数组中docs.ts
,我用*.routes.ts
. 我是否必须将其更改为*routes.js
.
谢谢你!
解决方案
发现这个 Github 问题可以解决我的问题。
推荐阅读
- css - 当孙子具有 will-change 属性时,无法访问 Safari(仅限)滚动条
- javascript - 打字稿:我如何从类型中定义严格的常量
- python - PyInstaller:“未能执行脚本”错误
- android - ActionBarDrawerToggle(Activity 活动, DrawerLayoutdrawerLayout, 工具栏工具栏, int openDrawerContentDescRes, int closeDrawerContentDescRes)
- java - 使用 Prim 和堆计算 MST 的总重量
- laravel - 如何从 laravel 中具有父 ID 的集合创建嵌套数组?
- c++ - UI 自动化示例错误:LNK2001 无法解析的外部符号 _CLSID_AccPropServices
- c# - 为什么 Visual Studio 在 Razor (.cshtml/.razor) 中突出显示双花括号?
- javascript - 当图像大小不同时,如何在 carasoul 中制作响应式图像?
- javascript - 来自javascript的二维码阅读器