🌐Middleware
Also known as Web Middleware, Request Middleware, Middleware Function
Code that runs on each request, one step at a time, before the route gets it.
Think of it like
It is like the checks at an airport. Each person goes past the same desks to get to the gate.
Example
Emma adds a login check as middleware. Now each page under /account turns away people who are not signed in.
Why it matters
Jobs that each route needs are written once, not copied into each one. Logs, logins and limits often live here.
Definition
Example code
javascript
app.use((req, res, next) => {
console.log(req.method, req.url);
next();
});Common mistake
Key takeaways
- 1Runs on every request. Before the route gets it.
- 2One job each. Log, check, then pass it on.
- 3Order matters. The stack runs top to bottom.