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

时间:2019-03-21 04:33:54

标签: node.js express routes 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}`));

4 个答案:

答案 0 :(得分:2)

尝试以下配置将swaggerUiexpress-app挂钩


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


答案 1 :(得分:1)

router.use('/jobs/api/:id',swaggerUi.serve,(req,res)=>{
console.log("here")
let a = req.params.id
if(a==='all'){ res.status(200).send(swaggerUi.generateHTML(swaggerDocument))}
if(a==='google'){res.status(200).send(swaggerUi.generateHTML(GoogleAds)) }

});

答案 2 :(得分:1)

我还没有深入研究swagger-ui-express,但是我认为问题出在函数generateHTML(在swaggerUi.setup中调用)中。全局模块变量(swaggerInit)在调用时会更新。

因此,对generateHTML的最后一次调用会对使用swaggerUi.setup中间件的每条路由产生副作用。

一种快速的解决方法是每次调用路由时都生成HTML。根据您提供的代码片段,它应该类似于:

 let swaggerDocEdi = require('./edi-openapi.json');
 let swaggerDocEcom= require('./ecom-openapi.json');
 let router = express.Router();

 router.use('/api/edi', swagger.serve, (req, res) => {
     let html = swagger.generateHTML(swaggerDocEdi);
     res.send(html);
 });

 router.use('/api/ecom', swagger.serve, (req, res) => {
     let html = swagger.generateHTML(swaggerDocEcom);
     res.send(html);
  });

请注意,全局变量仍在更新。

答案 3 :(得分:0)

似乎Router的使用不正确。对于这个简单的用例,建议您将路由直接添加到app实例中。参见:

const express    = require('express');
// xxxx 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
app.use('/', swaggerUi.serve); // replaced router with app

// Route - EDI RESTful API Documentaion 
// REPLACED "router" with "app"
app.get('/edi', swaggerUi.setup(ediSwaggerDocument)); 
// Route - eCommerce RESTful API Documentaion 
// REPLACED "router" with "app"
app.get('/ecom', swaggerUi.setup(ecomSwaggerDocument));

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

现在,可以通过将Routerapp一起添加到app.use()实例中来使用// Route - EDI router.get('/edi', swaggerUi.setup(ediSwaggerDocument)); // Route - eCommerce router.get('/ecom', swaggerUi.setup(ecomSwaggerDocument)); // Adding it to App instance app.use('/swagger', router) // End Point => localhost:5000/swagger/edi 。参见:

py_compile.compile(path+'xxx.py')

希望这会有所帮助!