使用 Swagger 记录 TypeScript 转译的 API

问题描述 投票:0回答:0

我安装了

swagger-jsdoc
swagger-ui-express
。然后我创建了UI的选项文件,写了
apis
的相对地址。然后在我初始化我的 API 服务的地方调用选项并将它们放入
specs
以将其插入
swaggerUi.setup
。用户视图已正确创建,但我在 UI 中看不到端点。

enter image description here

选项文件:

export const options = {
    swaggerDefinition: {
      openapi: '3.0.0',
      info: {
        title: 'MEnvio API',
        version: '1.0.0',
        description: 'Doumentación de los enpoints de la API del Backend del sistema MIEnvio',
      },
      servers: [
        {
          url: 'http://localhost:1802',
        },
      ],
    },
    apis: ['./routes/*.js'],

      };

服务器应用程序:

import express, { Application, json, urlencoded } from 'express';
import dotenv from 'dotenv';
import cors from 'cors';
import usersRoute from './routes/usersRoute';
import viajeRoute from './routes/viajeRoute';
import swaggerUi from 'swagger-ui-express';
import swaggerJsDoc from 'swagger-jsdoc';
import { options } from './swaggerOptios';
class Server {
  private backend: Application;
  private userRouter: usersRoute;
  private viajeRoute: viajeRoute
  constructor() {
    dotenv.config();
    this.backend = express();
    this.userRouter = new usersRoute();
    this.viajeRoute = new viajeRoute();
    this.config();
    this.route();
    this.start();

  }
  public config = () => {
    this.backend.set('port', process.env.PORT);
    this.backend.use(urlencoded({ extended: true }));
    this.backend.use(json());
    this.backend.use(cors());
    const specs = swaggerJsDoc(options);
    console.log(specs);
    this.backend.use('/docs',swaggerUi.serve, swaggerUi.setup(specs));

  }
  public route = () => {
    this.backend.use(`${process.env.USERSROUTE}`, this.userRouter.router);
    this.backend.use(`${process.env.USERSROUTE}`, this.viajeRoute.router);
  }
  public start = () => {
    this.backend.listen(process.env.PORT, () => {
      console.log('Server on port:', process.env.PORT);
    });
  }
}
const server = new Server();

端点文件:

import { Router } from 'express';
import userController from '../controller/usersController';
class usersRoute {
  public router: Router;
  public controller: userController;
  constructor() {
    this.router = Router();
    this.controller = new userController();
    this.config();
  }
  public config() {
    this.router.post('/login', this.controller.login);
    this.router.post('/register', this.controller.register);
    /**
    * @swagger
    * /getDataUser:
    *  get:
    *    summary: Returns a list of tasks
    */
    this.router.get('/getDataUser/:id', this.controller.getUserData);
    this.router.post('/updateUser', this.controller.updateUser);
  }
}
export default usersRoute;

我正在 dist 文件夹中转译为 JS:

enter image description here

我以为在Swagger UI中可以看到端点,但是你看不到它。

typescript swagger swagger-ui
© www.soinside.com 2019 - 2024. All rights reserved.