Refactor the Controller
Explore how to refactor controller route handlers in an Express RESTful API by extracting validation logic into middleware. Understand using the res.locals property to store request-specific data and update handlers to utilize the middleware for improved code organization and error handling. Test the changes to ensure your API endpoints correctly handle recipe resources without duplicated code.
We'll cover the following...
We'll cover the following...
Refactor the recipes controller
Currently, the get(), update(), and remove() route handlers repeat the following chunk of logic to check that a recipe exists in the database and to throw an error if the recipe doesn’t exist:
To avoid duplicating the logic across controllers, we can move the validation logic into a middleware ...