DEV Community

Cover image for Explain Middleware like I'm five
Peter Kim Frank
Peter Kim Frank Subscriber

Posted on

Explain Middleware like I'm five

Cover image courtesy of Will Francis via Unsplash

Top comments (9)

Collapse
 
jacobherrington profile image
Jacob Herrington (he/him) • Edited

It's the stuff between the leftware and the rightware.

Actually, it's a pretty vague and broad definition.

Middleware is kind of like a translator or a mediator; it helps two or more "things" communicate with each other.

Sometimes we call it "glue code" because it usually connects two software projects that need to work together.

Some examples might include: a database and a web application, a network and a server, or an operating system and an application.

My analogy for a five year old: When you brush your teeth, your toothbrush is the middleware between the toothpaste and your teeth. We could just pour toothpaste in your mouth and call it done, but using a toothbrush is easier and very helpful.

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. :-)

Collapse
 
alohci profile image
Nicholas Stimpson • Edited

You've been playing with your toys and they're scattered all over the room. Mummy tells you to put them all away in their toyboxes, but there's loads of them, so you ask daddy to help you. You pick up each toy and give it to daddy who collects them up, checks that they're still working, figures out which box they go in and puts them away. Daddy is your middleware.

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch

Speaking of Rails middleware like Rack or enterprise middleware like MQ?

Collapse
 
nestedsoftware profile image
Nested Software

or even redux middleware in react... so many uses of that term, all similar in some sense, but probably different enough if you are specifically trying to understand that technology...

Collapse
 
kspeakman profile image
Kasey Speakman

They are like browser extensions. Your web server often needs some plugins to do specific things like authentication or CORS. Middleware plugs into the web server to provide this functionality. It's called Middleware because it is code that is placed in between the web server and your app code. So the web server's request passes through it and sets up auth or CORS before it gets to your code.

Collapse
 
yoadev profile image
Younes

In general it is more like a piece of code that will run between two task. For example, if you want to check the authentication everytime someone wants to access a webpapge before sending the page, you would have a middleware that would handle this auth checking everytime. This way, you only have one auth system that handles every connections you want. And you can remove this need of auth just by removing this middleware for a specific page par example.

Don't know if it is clear or 100% accurate so don't be shy and correct me 😁

Collapse
 
tilkinsc profile image
Cody Tilkins

Middleware is using a pancake fryer instead of a griddle.

Collapse
 
blent_levent_37a3136e1cc profile image
Bülent Levent

Thanks.. bebelik.com