Middleware is a piece of software that sits between a client request and the core application logic, handling tasks such as authentication, logging or data transformation. It allows different parts of a system to communicate without each component needing to know the details of the others. By centralising these cross‑cutting concerns, developers can keep their main codebase focused on business rules.
Also called: intermediate layer, software glue
Why it matters
For a client paying for a website, middleware means faster delivery and fewer bugs because common functions are handled in one place rather than duplicated across pages or services. It also makes future changes, like adding a new payment provider, less disruptive and cheaper to implement.
How it works
When a request reaches a server, the framework passes it through a pipeline of middleware classes or functions. Each piece receives the request object, can read or modify it, perform side‑effects such as writing to a log, and then calls the next middleware in the chain. In Laravel this is defined in the $middleware array, while in Express.js it is added with app.use(). The final handler produces the response that is sent back to the client.
The mistake people make
A common error is putting heavy business logic or long‑running operations inside middleware, which blocks the request pipeline and slows every page load. Developers also forget to call the next function, causing the request to hang and resulting in a site that appears to be down.