Skip to content

Middleware Stack

You’re driving down the highway. You hit a Security Checkpoint. You stop, they check your ID, scan your cargo, and if everything looks good, they wave you through to the next section. Middleware functions in Express are exactly like these checkpoints.

They sit between the raw request coming in and the final route handler. They can turn back the car (send a response immediately), inspect the trunk (modify req), or just wave it through to the next booth (call next()).

A high-tech digital toll booth / security checkpoint. Scanners shooting laser grids over passing data packets.

Fig 1: Inspect, Process, Pass.


Middleware functions take three arguments: req, res, and next.

next is a function that, when called, passes control to the next middleware function in the stack.

Professor Solo’s Directive:

If you don’t call next() or send a response, the request hangs forever. The driver sits there honking until the connection times out. Don’t be that guy.


Express Middleware Guide

⏭ Custom Checkpoints

Let’s build our own toll booth.