I am trying to document an API, for which i use swagger-ui-express
.
When I use the following,
app.use('/api-docs/*', swaggerUi.serve, swaggerUi.setUp(swaggerDoc)
or
app.use('/api-docs/*', swaggerUi.serve)
app.get('/api-docs/*', swaggerUi.setup(swaggerDoc))
Everything works perfect, But I want the swaggerDoc
to get created dynamically based on the URL hit, hence I add the following code. But now, the HTML page never renders in the browser but when hit in Postman, it seems to have the same response body as when the HTML rendered for the previous method.
So when I modify the above code as below,
app.use('/api-docs/*', swaggerUi.serve)
app.get('/api-docs/*', function(request, response, next){
console.log(request.url);
var apiNameSplitArray = request.url.split('/')
var apiName = apiNameSplitArray[2]
swaggerDoc = JSONConstructor.JSONConstructorTest(apiName.trim())
next()
})
app.get('/api-docs/*', swaggerUi.setup(swaggerDoc))
It does not work(does not get rendered in the browser). When I checked the control flow using breakpoints, I noticed the following method execution order
app.use
- 2nd
app.get
- 1st
app.get