node.js - 多个 swaggerUi 与一个 express 应用程序

标签 node.js typescript express swagger openapi

我正在尝试找到一种解决方案,使单个 Express 应用程序拥有多个 swaggerUi 文档。

我正在使用:

"typescript": "^2.5.2",
"swagger-tools": "^0.10.1",
"express": "^4.15.3",
"express-openapi": "^1.0.1",

我的 swagger doc 文件部分是用项目文件架构生成的。

我该怎么做?

编辑---

现在我正在像这样初始化 swaggerUi :

const openapi = Openapi.initialize({
    paths: openApiPaths,
    expressApp,
    swaggerApiDoc,
  });
const openApiSpec: any = openapi.apiDoc;
app.use(swaggerUI(openApiSpec));

其中 openApiPaths 包含 swagger 文档的 paths:{}

最佳答案

自 2022 年起,您可以向同一个应用程序添加多个 swagger UI。 来自swagger-UI documents :

To run 2 swagger ui instances with different swagger documents, use the serveFiles function instead of the serve function. The serveFiles function has the same signature as the setup function.

const express = require('express');
const app = express();
const swaggerUi = require('swagger-ui-express');
const swaggerDocumentOne = require('./swagger-one.json');
const swaggerDocumentTwo = require('./swagger-two.json');

var options = {}

app.use('/api-docs-one', swaggerUi.serveFiles(swaggerDocumentOne, options), swaggerUi.setup(swaggerDocumentOne));

app.use('/api-docs-two', swaggerUi.serveFiles(swaggerDocumentTwo, options), swaggerUi.setup(swaggerDocumentTwo));

关于node.js - 多个 swaggerUi 与一个 express 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48339325/

相关文章:

javascript - 使用 gulp 运行命令以启动 Node.js 服务器

javascript - 覆盖返回对象中的字段 sequelize 模型 nodejs

typescript - 如何在接口(interface)上扩展属性

typescript - 如何在 Typescript 类中合并客户端传递的配置对象和默认配置对象?

angular - 模块 'AnyComponent' 声明的意外值 'AppModule'

javascript - 如何在 NodeJS 的 post 请求中发出 socket.io 响应

node.js - 使用 Model.find() 调用 MongoDB 时 KeystoneJS 中间件运行两次

node.js - Cassandra 向已关闭的 Node 请求数据?

javascript - 如何在express js(nodejs)中启动应用程序? TypeError :app. 设置不是一个函数

node.js - 特拉维斯 CI : Karma testing passing but always get errored build