setup multiple swagger url in node js application

·

2 min read

When we start work on some type of large application and we are going to setup swagger in our application in this case we require different endpoints in our swagger to look each end point in a better way. Like we have user/mobiles and some other endpoints in our application in this case we can setup our swagger with different url's for different end points.

Please refer below video to understand how we can setup a swagger in node/express js application:

app.use('/usersswagger', swaggerUi.serve, swaggerUi.setup(swaggerUsersConfig));

above code will work fine in case we need single swagger url in our application and we will display all the data in the same page.

Note: above code only work for single swagger route. in case you will pass two different routes and config in the two statements like this.

app.use('/usersswagger', swaggerUi.serve, swaggerUi.setup(swaggerUsersConfig));
app.use('/mobilesswagger', swaggerUi.serve, swaggerUi.setup(swaggerMobilesConfig));

above code will create a single reference of both of the statements and we will see the same contents in both of the swagger url.

in case we want different swagger routes in our application like for user it will be /usersswagger and for mobile end point it will be /mobilesswagger. we will have to follow below code it will create different reference of our configuration we are passing swaggerUsersConfig and swaggerMobilesConfig in the setup method and we will be able to see the different content in our swagger url.

app.use("/usersswagger", swaggerUi.serve, (...args) => swaggerUi.setup(swaggerUsersConfig)(...args));
app.use("/mobilesswagger", swaggerUi.serve, (...args) => swaggerUi.setup(swaggerMobilesConfig)(...args));