3
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 Answers1

3

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);
 }
});
  • Hi, did this work ? I am trying similar thing except that the swaggerDocument is in itself chosen inside the callback based on the URL. I get it working one time and the other time it does not, I really cannot get a grip on it. Do you have any suggestions ? – Reshma Suresh Mar 14 '19 at 07:03