Skip to content
tezvyn:

Express Middleware: The Chain of Command for Requests

Source: expressjs.comMediumHow cards are made

Express Middleware: The Chain of Command for Requests

Express middleware is a chain of functions a request passes through. Each function can inspect, modify, or stop the request, useful for logging, auth, or body parsing.

Why it exists

Express is a minimal framework by design. It needed a structured, composable way to handle tasks common to many requests—like logging, authentication, or data parsing—without cluttering the final route handler. Middleware provides this ordered pipeline for processing requests.

The mental model

An Express application is a pipeline of functions, not just a collection of endpoints. A request enters one end and flows through each middleware in sequence. Each function is a gatekeeper with three choices: first, modify the request or response and pass it on by calling next(); second, end the request by sending a response (e.g., res.json()); or third, pass control to an error handler.

How it works

A middleware function is a function with access to the request object (req), the response object (res), and the next function. It can execute any code. For example, an authentication middleware might verify a token and attach the user's data to the request object, like req.user = userData. To continue the processing chain, it MUST call next(). To stop the chain and reply to the client, it calls a response method like res.send(). If a middleware does neither, the request hangs. Middleware is loaded using app.use() for all requests or app.METHOD() for specific HTTP verbs and paths.

When to use it

Use middleware for any logic that needs to run before your main route handler, especially for shared, cross-cutting concerns. Common uses include: first, logging request details; second, parsing incoming data with express.json() or express.urlencoded(); third, authenticating and authorizing users; and fourth, serving static files from a directory.

When not to use it

Avoid putting unique business logic for a single endpoint inside a generic, globally-applied middleware. If logic only applies to POST /products and nowhere else, it belongs in that specific route handler. Middleware is for reusable concerns, not one-off endpoint logic.

One canonical example

A simple request logger is the classic "hello world" of middleware. Placed at the top of your application, it runs for every incoming request.

app.use((req, res, next) => {
console.log('Time:', Date.now(), 'Path:', req.path);
next();
});

This function logs the timestamp and path, then crucially calls next() to pass control to the next function in the chain, allowing the request to be processed further.

Interview question

What is the consequence if an Express middleware function neither calls next() nor sends a response?

  • a.The client's request will hang indefinitely, awaiting a response.Correct
  • b.The server terminates the connection with an internal server error.
  • c.The application automatically proceeds to the next middleware in the sequence.
  • d.The request is silently discarded, and the client receives no feedback.
Why?

The card states, "If a middleware does neither, the request hangs." This means the client will wait indefinitely for a response. Calling next() is explicitly required for the request to proceed to the next function in the chain, making option C incorrect.

Just read this? Test yourself on what you have been reading.

Read the original → expressjs.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on express — each one lists the topics its interview covers.

See open roles