Closed mahdiHash closed 1 year ago
Apparently, the problem was using localhost
instead of 127.0.0.1
!
I don't know why this happened.
I am having the same or similar issue - not rendering in express and I have tried localhost and 127.0.0.1. Incidentially, my specific error is:
SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data
I am having the same or similar issue - not rendering in express and I have tried localhost and 127.0.0.1. Incidentially, my specific error is:
SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data
Since it's a SyntaxError
, there's probably something wrong with your .json file.
I figured out what the issue was! This is important! Make sure the Content-Type is text/html (sent via headers from Express) - the trick for me was to explicitly move the route to the highest order possible and prior to me setting the standard 'application/json' Content-Type that I needed for the rest of the express back-end. It's amazing how such a little change solved such a major issue. Hope it helps.
Swagger-ui-express
v4.6.0
. Openapiv3.0.0.
. Expressv4.18.2
. Brave Browserv1.47.186
(Chromiumv109.0.5414.119
).I'm using Swagger-ui-express (without JSDOC) in my Express.js app to write documentation for my API. Here's the routing and middleware code (I use
swagger.josn
instead of writing comments):When I go to the route specified for reading the documentation, the page is blank and there's nothing rendered:
As you can see, the route and the swagger-ui-express middleware are working (there's "Swagger UI" and its icon on the tab's title), but I have no idea why it's not rendering anything.
There's also an error in the browser console:
I'm using OpenAPI (Swagger) Editor VSCode extension to see the preview of documentation and it's rendering OK, which means everything is syntactically correct:
I sent a request via Postman and I got this:
I opened
swagger-ui-init.js
file in the browser dev tools and I got the same code as the response I got in Postman. I think theswaggerUI
middleware is putting wrong code insideswagger-ui-init.js
.Could this problem be fixed? Am I doing anything wrong here?