spencerlepine / woofer

Dating app for pets - a full stack MERN project. Complete with CI/CD pipeline w/ Jest, GitHub Actions, Docker Hub, and AWS EC2
https://youtu.be/aiJhCoZRc78
2 stars 1 forks source link

Modularizing API Endpoints #74

Closed spencerlepine closed 2 years ago

spencerlepine commented 2 years ago

Situation:

Endpoints are pulling records from several database collections for each user. Currently, the endpoint controller logic is a messy and hard to debug, with many promises entangled. It is complex to write unit/integration tests with messy code and vague functions. Currently, there is no good way to handle errors and responses, with so many nested promises and callbacks.

Task:

Redesigning and modularizing the API endpoints. Improving the response time and refactoring code to be more readable. The goal is to have clear separation of concerns.

Action:

Step 1: Using Promises Refactored controllers to use promises ONLY, and removed scattered use of callback functions.

Step 2: Modular Controllers Created smaller files with specific modular functionality. This made it much easier to write unit tests for each controller or helper function.

Step 3: Refactor Controllers Refactored the API controller functions to handle errors properly. If an error occurs at any point, it will pass it up the Promise chain. Before, I was passing down the res​ object through function arguments, which resulted in res Headers being sent to the client after the endpoint had responded.

Result:

The backend code base was WAY easier to follow. Each controller had clearly defined functions, as well as helper functions that had ONE concern. It was was easier to write tests for a controller and controllerHelper function