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

请帮我解决这个问题..

4

1 回答 1

2

找到了解决办法,

像这样使用回调函数,

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);
 }
});
于 2018-11-21T10:02:24.000 回答