Member-only story
How to Make A Map Generator Using The MERN Stack And TypeScript — Part 4: Improving the API
Let’s make it clean and usable
Goal
This part is the final part on the backend side. We’ll clean up everything we’ve done so far to make it more easy to understand. We’ll add parameters to our API so we can try out different values, for size, colors and random bounds, without having to recompile everytime. Finally, we’ll make sure the map changes only if we tell it to.
API Cleanup
At the moment, the code for generating the map lies in the controller. This works well, but this is not a really clean practice. Ideally, the a controller should only responsible to do 4 things:
- Read the API parameters
- Call business functions that will do something with those parameters
- Return the API response
- Treat errors if any and return appropriate response
And at the moment, it does much more than that.
So let’s create a new folder called business inside our app folder, and create a mapBusiness.ts file. We’ll pretty much just move all the generation logic from the controller inside that file. It will look like this: