Middleware in Express.js
# CHAPTER 7
Middleware in Express.js
1. Introduction
If you understand Middleware, you understand Express.js. Middleware functions are the unsung heroes of your application. They are functions that intercept incoming HTTP requests, inspect them, modify them, log them, or block them entirely before they ever reach your core routing logic. In this chapter, we will demystify middleware, explore built-in Express tools, and write our own custom interceptors.2. Learning Objectives
By the end of this chapter, you will be able to:- Define the concept and anatomy of an Express Middleware function.
-
Understand the critical importance of the
next()function.
-
Implement Global Middleware using
app.use().
- Implement Route-Specific Middleware (e.g., protecting a route).
3. Beginner-Friendly Explanation
Imagine an exclusive VIP nightclub.- The Customer: The incoming HTTP Request.
-
The Dance Floor: Your API Route Logic (
res.json(...)).
- The Bouncers: The Middleware.
Before the customer can reach the dance floor, they must walk down a hallway lined with bouncers.
-
1.
Bouncer 1 (Logger): Writes down the customer's name and arrival time in a ledger, then says "Go ahead" (
next()).
-
2.
Bouncer 2 (Security): Checks if the customer is carrying anything dangerous. If safe, says "Go ahead" (
next()).
-
3.
Bouncer 3 (Authentication): Checks if the customer has a VIP wristband. If they do, they enter the dance floor. If they don't, the bouncer physically throws them out the front door (
res.status(401)), and they never reach the dance floor.
4. Anatomy of a Middleware Function
A middleware function looks exactly like a standard route, but it has a third argument:next.
5. Global Middleware
To apply middleware globally (to every single route in your application), we useapp.use().
Let's build a Custom Logger:
6. Route-Specific Middleware
You don't want a security check to run globally; guests need to see the public homepage! You only want the security check to run on private routes.Let's build a Fake Authentication Middleware:
*To test this: Visiting /api/dashboard fails with 401. Visiting /api/dashboard?apikey=supersecret_123 succeeds with 200.*
7. Built-in Express Middleware
Express comes with powerful middleware out of the box. The most important one isexpress.json(). If a client sends a JSON payload to your server, Express is blind to it by default. You must add app.use(express.json()) at the top of your file to tell Express to intercept the payload and attach it to the req.body object.
8. Backend Workflow: Modifying the Request
Middleware can do more than just block traffic; it can alter the request before the Route sees it. For example, an Authentication middleware can find the logged-in user in the database, and attach the user object directly to the request:req.user = dbUser; next();. Now, the final Route instantly knows exactly who is logged in!
9. Best Practices
-
Order of Execution: In Express, order matters absolutely. Express reads your
app.jsfile from top to bottom. If you define a Route *above* your Global Logger Middleware, the Request hits the route, sends the Response, and the Logger is entirely ignored! Always putapp.use()Global Middlewares at the very top of your file.
10. Common Mistakes
-
Forgetting
next(): If your custom middleware doesn't end withres.json()(throwing an error) ornext()(passing it on), the request becomes trapped in purgatory. The client's browser will spin indefinitely.
11. Exercises
-
1.
Explain the "Bouncer Analogy" and how the
next()function operates within an Express application. What happens ifnext()is never called?
12. Coding Challenges
-
Challenge: Write a custom route-specific middleware called
checkAge. It should check a query string?age=X. If the age is less than 18, it should return a 403 Forbidden status with a JSON error message. Apply this middleware to a route named/api/restricted-content.
13. MCQs with Answers
In an Express middleware function (req, res, next), what happens if you successfully validate the request but forget to call the next() function?
How do you apply a middleware function so that it intercepts EVERY incoming HTTP request to your entire Express application?
14. Interview Questions
- Q: Explain the Request-Response lifecycle in Express, specifically detailing how multiple middleware functions (a "middleware stack") are executed in sequence.
-
Q: Provide a real-world scenario where a custom middleware would modify the
reqobject before callingnext(), and explain how the subsequent route utilizes that modification.
15. FAQs
Q: Can I use third-party middleware? A: Absolutely! The Node ecosystem provides amazing pre-built middleware via NPM. Examples includecors (for cross-origin requests), helmet (for security headers), and morgan (for professional logging).