swagger-ui-express Multiple Routes for Different API Documentation

寵の児 提交于 2020-02-25 01:33:50

问题


I have 2 separate swagger API documentations which I want to run via swagger-ui-express NPM package, and my express server is starting fine on port 5000, but when I am trying to access any of the URL always getting the 404 error, Here is my app.js file and URL's for your reference:

Route 1: http://localhost:5000/edi Route 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}`));

回答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:


Short answer, yes.

Try the following configurations to hook swaggerUi with express-app


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





回答3:


It looks like the Router is being used incorrectly. For this simple use case I would recommend adding your routes directly to the app instance. See:

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}`));

Now, you could use the Router by adding it to the app instance with app.use(). See:

// 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

Hope this helps!!



来源:https://stackoverflow.com/questions/55273857/swagger-ui-express-multiple-routes-for-different-api-documentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!