DEV Community


Discussion on: Question: What is middleware in web development?

andreidascalu profile image
Andrei Dascalu

A Middleware is a bit of code (can be a function or another application) that executes before the code meant to handle a given operation.

Imagine a classic setup where you have a some controllers each handling a given uri.

To make sure there authentication check for all of them, instead of beginning each controller with an if 'authenticated' do this else return unauthorized, you would use a Middleware which would return the unauthorized early else pass execution to your controller.

In frameworks this is usually done for you behind the scenes via configuration in some common cases