Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve out swagger-ui from nodejs/express project

I would like to use the swagger-ui dist 'as-is'...well almost as-is.

Pulled down the latest release from github (2.0.24) and stuck it in a folder in my app. I then server it out statically with express:

app.use('/swagger', express.static('./node_modules/swagger-ui/dist'));

That works as expected when I go to:

https://mydomain.com/swagger

However I want to populate the url field to my swagger json dynamically. IE I may deploy to different domains:

https://mydomain.com/api-docs
https://otherdomain.com/api-docs

And when I visit:

https://mydomain.com/swagger
https://otherdomain.com/swagger

I would like to dynamically set the url.

Is that possible?

like image 617
lostintranslation Avatar asked Sep 17 '25 19:09

lostintranslation


1 Answers

Assuming the /api-docs (or swagger.json) are always on the same path, and only the domain changes, you can set the url parameter of the SwaggerUi object to "/path/to/api-docs" or "/path/to/swagger.json"instead of a full URL. That would make the UI load that path as relative to the domain the UI is hosted on.

For reference, I'm leaving the original answer as well, as it may prove useful in some cases.

You can use the url parameter to set the URL the UI should load.

That is, if you're hosting it under https://mydomain.com/swagger you can use https://mydomain.com/swagger?url=https://mydomain.com/api-docs and https://mydomain.com/swagger?https://otherdomain.com/api-docs to point at the different locations.

However, as far as I know, this functionality is only available at the current alpha version (which should be stable enough) and not with 2.0.24 that you use (though it's worth testing).

like image 91
Ron Avatar answered Sep 20 '25 08:09

Ron