Skip to content

What it is

Middleware is software that sits between the request and the response, transforming or filtering the traffic before or after the handler runs.

A middleware is a function (or object) that receives a request, may modify it, may short-circuit it (returning a response without the handler running), may call the next middleware in the chain, and may modify the response on the way back. The endpoint handler is the last function in the chain; middlewares wrap it.

Express, Koa, Laravel, ASP.NET Core, Django and Rails all use the same model under different names: app.use(fn) in Express and Koa; middleware in Laravel and ASP.NET Core; middleware classes in Django; before_action/around_action in Rails. The concept is older than any of them: it dates to the 1960s, when middleware was the software layer between the OS and the applications.