Middleware
ELI5 — The Vibe Check
Middleware is like a security checkpoint at an airport. Before your request reaches its destination (the controller), it passes through multiple checkpoints — one checks your auth token, one logs the request, one validates the data. Each checkpoint can stop you or wave you through.
Real Talk
Middleware is a function that sits between the request and the final route handler. It has access to the request and response objects and can execute code, modify them, end the cycle, or call the next middleware in the stack.
Show Me The Code
// Auth middleware
const requireAuth = (req, res, next) => {
if (!req.headers.authorization) {
return res.status(401).json({ error: 'Unauthorized' });
}
next(); // pass to next middleware
};
When You'll Hear This
"Add auth middleware to protect that route." / "The logging middleware runs on every request."
Related Terms
Authentication (AuthN)
Authentication is proving you are who you say you are.
Controller
A controller is the manager who actually handles your request after it passes through security.
Express
Express is the most popular framework for building Node.js backends.
Route
A route is like a road sign that tells incoming requests where to go.
Router
The router is the traffic cop of your backend.