DEV Community

Discussion on: Explain Middleware like I'm five

Collapse
 
mattcobley profile image
Matt Cobley

Imagine you're looking to cook a meal. You get the ingredients and then you have to prepare them and then apply a recipe. Middleware is like the preparation stage. You can do it all by hand, or you can stick some of the ingredients in a machine which can do some of it for you, and you can combine different tech in different ways to get it ready for the main thing you're looking to do, but the main cooking happens once you have gotten the ingredients ready and prepared. The main cooking is analogous to how you might handle a network request on a REST endpoint once it reaches a controller method, but he middleware is how you can process that request before it gets there. This might be to apply some validation logic and reject the request if it doesn't meet it, apply authentication and authorisation, perform logging, process aspects of the request, etc. Hopefully that's of some help. :-)