swagger-ui-express 不同 API 文档的多个路由

luv*_*ing 4 routes node.js express swagger-ui

我有 2 个独立的 swagger API 文档,我想通过 swagger-ui-express NPM 包运行它们,并且我的 Express 服务器在端口 5000 上正常启动,但是当我尝试访问任何 URL 时总是收到 404 错误,这里是我的 app.js 文件和 URL,供您参考:

路线 1:http://localhost:5000/edi 路线 2:http://localhost:5000/ecom

const express    = require('express');
const router     = require('express').Router();
const swaggerUi  = require('swagger-ui-express');

const ediSwaggerDocument  = require('./edi-openapi.json');
const ecomSwaggerDocument = require('./ecom-openapi.json');

const SWAGGER_APP_PORT = process.env.SWAGGER_APP_PORT || 5000;

const app = express();

// Route Middleware to be called before serving Any Route
router.use('/', swaggerUi.serve); 

// Route - EDI RESTful API Documentaion 
router.get('/edi', swaggerUi.setup(ediSwaggerDocument)); 

// Route - eCommerce RESTful API Documentaion 
router.get('/ecom', swaggerUi.setup(ecomSwaggerDocument));

app.listen(SWAGGER_APP_PORT, () => console.log(`RESTful API Up and Running on Port ${SWAGGER_APP_PORT}`));
Run Code Online (Sandbox Code Playgroud)

Roy*_*Lee 8

尝试以下配置来swaggerUi挂钩express-app


app.use("/edi", swaggerUi.serve, (...args) => swaggerUi.setup(ediSwaggerDocument)(...args));
app.use("/ecom", swaggerUi.serve, (...args) => swaggerUi.setup(ecomSwaggerDocument)(...args));


Run Code Online (Sandbox Code Playgroud)