Swagger driven API Development in Nodejs

Github link: https://github.com/arun1104/quiz-api

Arun Rajeevan
1 min readJul 7, 2019

Tool used: Visual Code

NPM Modules used:

  1. Swagger-tools
  2. js-yaml
  3. Express

Note: No need to use router middleware in the express. Instead, we use swagger based routing.

Architecture description:

  1. Request first goes to the swagger validator.
  2. If the proper request(with supported URL, allowed headers, request body with required attributes) comes in, swagger validator fills the req object with swagger metadata attribute which will contain the operation id which we have specified in the swagger file.
  3. This valid request then reaches the controller.
  4. If an invalid request comes in, swagger validator will call the next(err). This err object will contain the reason of failure and the status code as well.
  5. Therefore it is very important to have an error handler middleware to handle the errors and send valid error response back to the client.

--

--

No responses yet