Skip to content

Middleware

Middleware is software that runs between a request and the handler that produces a response, intercepting the traffic to authenticate, log, transform, throttle or reject it — without the endpoint knowing it exists.

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

  1. Auth before anything expensive

    Reject unauthenticated requests at the top. Logging an auth failure is fine; hashing a body is not.

  2. Tracing before rate limiting

    A rate-limited request should still be traced, so you can see the throttle in the trace.

  3. Rate limiting before the handler

    The handler should never run for a throttled request.

  4. 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
1960s · "middleware" como capa OS-aplicaciones
1990s · middleware empresarial (CORBA, Tuxedo, MQ)
2010 · Express populariza el middleware HTTP
2016 · ASP.NET Core formaliza el middleware pipeline
N · middlewares en una app mediana típica

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, called next() exactly once, or short-circuited with the expected response.

Connections

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

Middlewareweb-framewarchitectuRouteEndpointfilterinterceptocross-cutt

● broader ● narrower ● related

Broader

  • web-frameworks
  • architecture

Related

Sources