On my previous post How to setup Swagger UI for Web API I went through the process of integrating Swagger into an ASP.NET Web API project.
Today I'll go through how to setup Swagger UI for NodeJS.
Swagger has a node module which provides the necessary tools for designing and implementing APIs in NodeJS. I has a great integration with other NodeJS servers such as HapiJS, Restify, Sails, Connect, and any connect-base middleware.
Having Swagger integrated into your NodeJS application will give you the opportunity to build and rewrite your API until you are satisfied through multiple iteration of refining your API. What's really nice about Swagger module is that you don'y need to worry about any setups once installed for instance, it takes care of routing and validation, and the configuration setups are place in a single file.
Now, lets setup Swagger for NodeJS
Throughout the post, I'll been using Windows CLI, as I'm a Windows user, but that shouldn't change the way you call Swagger through any CLIs.
Install swagger through NPM
$ npm install -g swagger
Once Swagger has been install successfully, now we need use Swagger CLI to create and manager projects.
swagger project create emailapp
When running the previous command, you will prompted to select the Connect-base middleware. I'll select HapiJS.
Once it has install, you'll get this message :-)
Swagger has a built in interactive web-base Swagger Editor that can be viewable and editable through a web browser.
In order to edit project created use the following command.
swagger project edit
And that how it looks like.
And that how the EmailApp folder files structure looks like
In this step, I'll example how things are tied together, from Swagger Editor to the controller in the API.
At the start, lets understand how Swagger Editor tie to a specific controller.
In Swagger Editor you can specify the path of which the controller would map to.
As we can see on Line: 23, it is mapping to hello_world.js controller, also it has specified the accepted parameters, response and error. On the controller side, there are no changes to the way we normally implement our controller.
Once you are happy with, stop editing by stopping the Swagger project editor running process in the CLI.
Finally, lets start our swagger project and experiment with it.
swagger project start
That how it will look on your CLI.
Now, lets browse to this url provided so we can experiment.
Swagger UI is pretty helpful and handy when it comes to designing and developing your API, as it provides a way for experimenting and refinement of your implemented API. Swagger UI can be integrated into ASP.NET Web API and NodeJS as explained in this post and the previous post.