How to use swagger-ui-express.serve and setup swagger document inside call back function Nodejs

1.1k Views Asked by At
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');

app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

instead of this, i want to use inside callback function of NodeJS Need to set basepath dynamically inside callback function.

app.use('/swagger', function(req,res) {
  swaggerDocument.basepath = "/pet/details",
  res.send(swaggerUi.serve, swaggerUi.setup(swaggerDocument));
});

Please help me to resolve this..

1

There are 1 best solutions below

1
On BEST ANSWER

Found the solution,

Used callback function like this,

router.use(
 swaggerUi.serve,
 function(req, res) {
   swaggerDocument.host = req.get('host'); // Replace hardcoded host information in swagger file
   swaggerDocument.schemes = [req.protocol]; // Replace hardcoded protocol information in Swagger file
   swaggerUi.setup(swaggerDocument)(req, res);
 }
});