Building REST APIs with Swagger On Node.js

Nowadays REST APIs plays important role in every applications, Where it has been used to connect with other applications and to share data with that applications. REST APIs without a good documentation on how to use it, is useless. Because of that, developers have worked hard to create a standard way of describing APIs and documentation. Swagger On Node.js providing a reliable solution for design, development, testing and documentation of REST APIs.

Swagger is a powerful API developer tools for teams and individuals, enabling development across the entire API lifecycle, from design and documentation, to test and deployment. Swagger consists of a mix of open source, free and commercially available tools that allow anyone, from technical engineers to street smart product managers to build amazing APIs that everyone loves. For Building APIs With Node.js, Swagger provides official module for designing and building Swagger-compliant APIs entirely in Node.js. It integrates with popular Node.js servers, including Express, Hapi, Restify, and Sails, as well as any Connect-based middleware.

Swagger On Node.js Using Express

  • Install Swagger package globally, Using the command
    npm install -g swagger

    While installation you will be prompted for framework, Choose express

  • Create a new swagger project using CLI with command
    swagger project create hello-world
  • You will be created with swagger boilerplate project with directory structure as below,

    • api contains everything related to the API development(controllers, mocks, helpers and swagger). ~/swagger directory contains information of routes and its documentations.
    • config contains default.yaml that, as the documentation states, drives the application’s config directory.
    • test to test for REST APIs.
    • app.js is the main file which runs the server
  • You can edit your routes directly at ~/app/swagger or use interactive, browser-based Swagger Editor is built in. It provides Swagger 2.0 validation and endpoint routing, generates docs on the fly, and consumes easy-to-read YAML with command.
    swagger project edit
  • Code your API’s business logic in Node.js at controllers (~/api/controllers). All route will be linked with the controller with x-swagger-router-controller element at routes, Which specifies the name of the controller file associated with the /hello path. So, the controller source file for this project is ~/api/controllers/hello_world.js. The operationId element specifies which controller function to call, it is a function called hello.
  • You can run your project server, Using command
    swagger project start


    node app.js

Now, Your application is build with Swagger, Which make your application API design, development, documentation and maintenance work easier. If you have any comments/suggestions/feedback leave on below comment box.


SIVA SANKAR, Working as a Software Engineer, Blogging is my hobby. I completed my Bachelors of Engineering (Computer Science Engineering) in Chennai, India. and my Master of Engineering (Embedded System Technologies) in Tamilnadu, India.

2 thoughts on “Building REST APIs with Swagger On Node.js

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.