in swaggerui api restful nodejs node ~ read.
How to setup Swagger UI - NodeJS

How to setup Swagger UI - NodeJS

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.

Building API with swagger

Building your APIs with Swagger

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.

Step 1

Install swagger through NPM

$ npm install -g swagger

Step 2

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.

Select Connet-base middleware

Once it has install, you'll get this message :-)

Successfully created

Step 3

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.

Swagger Editor in the browser

And that how the EmailApp folder files structure looks like

Swagger Editor in the browser

Step 4

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.

Swagger Editor to Controller

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.

Swagger Editor to Controller

Once you are happy with, stop editing by stopping the Swagger project editor running process in the CLI.

Step 5

Finally, lets start our swagger project and experiment with it.

swagger project start

That how it will look on your CLI.

Swagger Project Start

Now, lets browse to this url provided so we can experiment.

Final result


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.

comments powered by Disqus