Swagger driven API Development in Nodejs
Tool used: Visual Code
NPM Modules used:
Note: No need to use router middleware in the express. Instead, we use swagger based routing.
- 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.