Swagger driven API Development in Nodejs
Github link: https://github.com/arun1104/quiz-api
1 min readJul 7, 2019
Tool used: Visual Code
NPM Modules used:
- Swagger-tools
- js-yaml
- Express
Note: No need to use router middleware in the express. Instead, we use swagger based routing.
Architecture description:
- Request first goes to the swagger validator.
- 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.
- This valid request then reaches the controller.
- 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.
- Therefore it is very important to have an error handler middleware to handle the errors and send valid error response back to the client.