1

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

  1. app.use
  2. 2nd app.get
  3. 1st app.get
Reshma Suresh
  • 165
  • 1
  • 4
  • 13

1 Answers1

3

I was also facing the same issue, After looking deeply found that the issue was related to incorrect response headers. In my case, the response header was set to res.setHeader('Content-Type', 'application/json');. After removing it worked.

Make sure your response header has Content-Type: text/html; charset=utf-8