代码之家  ›  专栏  ›  技术社区  ›  Daniel Netzer

Swagger ui express模块,仅实例化最后定义的文档

  •  6
  • Daniel Netzer  · 技术社区  · 6 年前

    我有一个类型化的nodejs服务器,我试图为不同的控制器定义不同的招摇过市路径,但招摇过市ui-express模块似乎只显示特定路由中最后定义的文档。

    指数X组控制器的ts

    import express from 'express';
    import passport from 'passport';
    const router = express.Router();
    // import all bot routes
    import { authRoute } from './auth';
    import { botCrudRoute } from './bot-crud';
    import { aiRoutes } from './ai';
    import { categoryCrudRoute } from './categories-crud';
    
    const swaggerUi = require('swagger-ui-express');
    import { botSwaggerDoc } from './swagger';
    
    const swaggerDoc = botSwaggerDoc;
    
    const swaggerUiOpts = {
        explorer: false
    };
    
    // Swagger setup
    router.use('/api-docs', swaggerUi.serve);
    router.get('/api-docs', swaggerUi.setup(swaggerDoc, swaggerUiOpts));
    

    指数Y组控制器的ts

    import express from 'express';
    const router = express.Router();
    // import all bot routes
    
    const swaggerUi = require('swagger-ui-express');
    import { adminSwaggerDoc } from './swagger';
    
    const swaggerDoc = adminSwaggerDoc;
    
    const swaggerUiOpts = {
        explorer: false
    };
    
    // Swagger setup
    router.use('/api-docs', swaggerUi.serve);
    router.get('/api-docs', swaggerUi.setup(swaggerDoc, swaggerUiOpts));
    
    export const adminRoutes = router;
    

    api。ts将所有控制器组分组

    'use strict';
    
    import express from 'express';
    import { Response, Request, NextFunction } from 'express';
    import { adminRoutes } from './admin';
    import { botRoutes } from './bot';
    // import { onboardRoutes } from './onboard';
    
    const router = express.Router();
    
    // router.use('/onboard', onboardRoutes);
    router.use('/bot', botRoutes);
    router.use('/admin', adminRoutes);
    
    export const apiRoutes = router;
    

    服务器ts

    /**
     * Primary app routes.
     */
    app.use('/api', apiRoutes);
    

    一个招摇过市的人的例子

    export const botSwaggerDoc = {
        'swagger': '2.0',
        'info': {
            'version': '1.0.0',
            'title': 'Cupo embed chat bot API',
            'license': {
                'name': 'Internal use only'
            }
    

    swagger ui express模块仅使用最后定义的文档,就像服务器保持对该文档的引用一样。。。

    1 回复  |  直到 6 年前
        1
  •  6
  •   John Huynh    6 年前

    我可以通过直接为每个单独的api提供HTML来解决这个问题。见下文:

    // index.ts for X group of controllers
    
      const apiV1Html = swaggerUi.generateHTML(
        v1SwaggerDocument,
      );
      router.use(
        '/docs',
        swaggerUi.serveFiles(v1SwaggerDocument),
      );
      router.get('/docs', (req: any, res: any) => {
        res.send(apiV1Html);
      });
      
      

    对于Y组控制器:

    // index.ts for y group of controllers
    
      const apiV2Html = swaggerUi.generateHTML(
        v2SwaggerDocument,
      );
      router.use(
        '/docs',
        swaggerUi.serveFiles(v2SwaggerDocument),
      );
      router.get('/docs', (req: any, res: any) => {
        res.send(apiV2Html);
      });
      
      

    资料来源: https://github.com/scottie1984/swagger-ui-express/issues/65