Node.js Backend web service/API. Part 3
You should take a quick look at part 1 and part 2 of this article because they are dependencies to better this part of the long boring article, I hope not.
You can skip this article if you can go through the source file here and understand everything you need. I know you can.
Utility classes and function and the concept behind them
We are going to discuss more on the files within the utils folder in the project structure.
- AppResponse
- ApiError
- Pagination
- Query Parser
AppResponse
This is a helper class that will be used to generate a formatted response that will be sent to the client. it will include a meta and data object, where the data object is the actual response data while the meta object will have more information about the data and could also have the error object should any occur.
AppError
This class extends the Error class with an overloaded constructor that takes extra argument peculiar to the app to format errors in a smarter way.
Pagination
There will be a need to pagination the result returned to the client. So to illustrate the concept a minimum requirement has been implemented in the pagination helper. it takes the request URL as parameter then resolves the query string special keys designated for pagination such as per_page, page, etc. and implemented functions required for pagination.
QueryParser
There was a need to parse the query object and extract information necessary to perform subsequent functions such as filtering, search and populations.
I believe we should now have a proper understanding of the project structure, the architecture and the basic function of each file within the utils folder. The part 4 will bring everything together by implementing the controller function within the app component.