Middleware
5 min read
Module Overview
What You’ll Learn
- Explain what Express middleware is and how it processes a request
- Use Express’s built-in middleware and well-known third-party packages
- Write your own custom middleware
💡 Why this module matters: Nearly everything an Express app does beyond a single route handler, parsing JSON bodies, logging requests, setting security headers, handling cookies, runs through middleware. Understanding the pattern unlocks how the rest of a real Express app is actually built.
What’s Covered
- Express middleware, the request-handling pipeline every request flows through.
- Built-in middleware, including
express.json(),express.urlencoded(), and static file serving. - Custom middleware, writing your own function to run on every matching request.
- Morgan, logging every incoming request.
- Helmet, setting security-related HTTP headers.
- CORS, controlling which origins can call your API.
- Cookie parser, reading cookies sent with a request.
- Compression, shrinking response sizes automatically.
Hands-On
By the end of this module, you’ll have built a custom logging middleware from scratch, and wired up a real Express app with JSON parsing, security headers, CORS, and cookie support.
Next module: error handling, what happens when something inside a route or middleware goes wrong.