The Express Request-Response Cycle Explained
Understanding the request-response cycle is the core of Express. Here is how it works.
The Express Request-Response Cycle Explained
Every Express API follows the same pattern: a request comes in, passes through middlewares, reaches a handler, and a response goes out.
The Cycle
- Request arrives: Express receives an HTTP request.
- Middlewares run in order: Each middleware gets (req, res, next). It can modify req, send a response, or call next to continue.
- Handler runs: The matched route's handler runs. It reads req, does work, and sends res.json or res.status.
- Response leaves: Express sends the response to the client.
What the Handler Sees
The handler is a function (req, res) => {}. It receives:
req.bodyparsed JSON body (after express.json middleware).req.paramsURL params like /users/:id.req.queryquery string like ?search=foo.req.headersHTTP headers.req.cookiesif cookie-parser middleware is used.
Sending a Response
res.status(201).json({ user })- send a status and JSON body.res.send('ok')- send plain text.res.redirect('/new')- redirect the client.res.cookie('token', jwt, options)- set a cookie before sending.
Middlewares in the Cycle
A middleware is (req, res, next) => {}. Common uses:
- Authenticate (set req.user, or send 401).
- Validate body (set errors, or call next).
- Log (call next at the end).
- Rate limit (send 429, or call next).
Middlewares run before the handler. They shape what the handler sees.
What Happens on Error
If a middleware or handler throws, Express skips to the error handler. Register an error-handling middleware at the end of app.js with four parameters: (err, req, res, next) => {}. It catches errors from any middleware or handler.
Example: POST /signup Cycle
- Request: POST /signup with JSON body.
- express.json middleware parses the body.
- validate middleware checks the body. If invalid, send 400. Else call next.
- Handler runs: check duplicate, hash password, create user, send 201.
- Response: { id, firstName, email }.
The Takeaway
The Express cycle is request, middlewares in order, handler, response. Middlewares shape req; handler reads req and sends res; errors jump to the error handler. Knowing this cycle is half of Express.
A request arrives, middlewares run in order (each can modify req, respond, or call next), the matched route handler runs, and the response leaves. Errors skip to the error handler at the end.
req.body (after express.json), req.params (URL params), req.query (query string), req.headers, and req.cookies (with cookie-parser). The handler reads these and sends a response.
Each middleware can modify req (e.g., set req.user from JWT), send an early response (e.g., 401 if auth fails), or call next to continue to the next middleware or handler. Middlewares run in the order you add them.
If a middleware or handler throws, Express skips to the error handler. Register an error-handling middleware with four parameters (err, req, res, next) at the end of app.js. It catches errors from any middleware or handler in the chain.
201 Created. The handler creates a user, so 201 is the correct status. Return the user object (without passwordHash) in the response body.
Ready to master Node.js completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master Node.js
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

