Middleware Stack
The Checkpoints
Section titled “The Checkpoints”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()).
Checkpoints on the Express Highway
Section titled “Checkpoints on the Express Highway”
Fig 1: Inspect, Process, Pass.
The next() Function
Section titled “The next() Function”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.
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.
Extra Bits & Bytes
Section titled “Extra Bits & Bytes”Express Middleware Guide