Definition
What it is
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.
Why it matters
Cross-cutting concerns multiply endpoints and drift when implemented per handler. Middleware centralizes them: one auth middleware, one logger, one rate limiter, one tracing span. New endpoints inherit the behaviour for free, and a change to the middleware applies to every endpoint at once.
Middleware vs filter vs interceptor vs decorator
Aspect | Middleware | Filter | Interceptor | Decorator |
|---|---|---|---|---|
Scope | Full request/response | Servlet filters | Method calls | One function |
Typical framework | Express, Laravel, ASP.NET Core | Spring, Java EE | NestJS, Angular | Python, TypeScript |
Can short-circuit | Yes | Yes | Yes | No |
Order matters | Yes | Yes | Yes | No |
The middleware chain
graph LR R[Request] --> A[Auth] A --> L[Logger] L --> T[Trace] T --> RL[Rate limit] RL --> H[Handler] H --> RL RL --> T T --> L L --> A A --> Resp[Response] style H fill:#E8F5E9
How to order middleware
Ordering middleware that works
Auth before anything expensive
Reject unauthenticated requests at the top. Logging an auth failure is fine; hashing a body is not.
Tracing before rate limiting
A rate-limited request should still be traced, so you can see the throttle in the trace.
Rate limiting before the handler
The handler should never run for a throttled request.
The order on the way back is reversed
Middlewares see the response in reverse order. If middleware A wraps middleware B, A's "after" code runs last.
Common failures
Origin
In the 1960s, "middleware" named the layer between the operating system and the applications: transaction monitors, remote procedure call libraries, message queues. The word was meant literally: it sat in the middle. In the 1990s, enterprise middleware (CORBA, MQSeries, Tuxedo) formalized the market. In the 2010s, web frameworks (Express, Koa, Laravel, ASP.NET Core) adopted the same term for the request pipeline: the middlewares sit between the HTTP server and the endpoint handler, and the metaphor survived intact.
An Express application is essentially a series of middleware function calls.
— Express.js documentation, *Using middleware*, 2024
Evidence
FAQ
- Middleware vs filter?
Semantically similar; different frameworks use different names. Middleware is the generic term; filter is the Servlet-specific one.
- Why does order matter?
Because each middleware wraps the next. A request goes in the declared order; the response goes out in reverse. A middleware that expects auth to have run first will fail if placed before it.
- Can middleware call the handler directly?
Yes, but it usually shouldn't; middleware calls
next()and the framework continues the chain.- How to test a middleware?
With a fixture request and a spy on
next(): assert the middleware modified the request, callednext()exactly once, or short-circuited with the expected response.
Connections
Express.js (2024). Using middleware. https://expressjs.com/en/guide/using-middleware.html
Microsoft Learn (2024). ASP.NET Core Middleware. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/
Laravel (2024). Middleware. https://laravel.com/docs/middleware
Etymology
Origin: compound
middle(noun): Old English middel, "the center, the space between"ware(noun): Old English waru, "goods, merchandise"; extended to software, hardware
Semantic network
● broader ● narrower ● related
Sources
Express.js (2024). Using middleware. https://expressjs.com/en/guide/using-middleware.html
Microsoft Learn (2024). ASP.NET Core Middleware. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/
Laravel (2024). Middleware. https://laravel.com/docs/middleware