如何在回调函数 Nodejs 中使用 swagger-ui-express.serve 和设置 swagger 文档

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

const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');

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

而不是这个,我想使用NodeJS的内部回调函数 需要在回调函数中动态设置basepath。

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

请帮我解决这个问题..

找到解决方案,

这样使用回调函数,

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