0
0
Expressframework~5 mins

Middleware execution flow (req, res, next) in Express - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is middleware in Express?
Middleware is a function that has access to the request (req), response (res), and next middleware function in the app's request-response cycle. It can modify req and res or end the request-response cycle.
Click to reveal answer
beginner
What does the 'next' function do in Express middleware?
The 'next' function passes control to the next middleware function in the stack. Without calling next(), the request will hang and not proceed.
Click to reveal answer
beginner
In what order are middleware functions executed?
Middleware functions are executed in the order they are added to the Express app or router. Each calls next() to pass control to the next middleware.
Click to reveal answer
intermediate
What happens if a middleware does not call next() or end the response?
The request will hang because Express waits for the middleware to either call next() or send a response. This causes the client to wait indefinitely.
Click to reveal answer
intermediate
How can middleware modify the request and response objects?
Middleware can add, change, or remove properties on req and res objects. For example, adding user info to req or setting headers on res before sending.
Click to reveal answer
What are the three parameters of an Express middleware function?
Areq, res, next
Brequest, response, callback
Creq, res, done
Drequest, response, nextMiddleware
What happens if you forget to call next() in a middleware?
AThe next middleware runs automatically
BThe request hangs and never completes
CExpress throws an error immediately
DThe response is sent automatically
Which middleware runs first in Express?
AThe last middleware added
BMiddleware added in random order
CMiddleware added first
DMiddleware with the shortest name
How can middleware modify the response before sending it?
ABy calling next() twice
BBy changing req properties only
CMiddleware cannot modify response
DBy changing res properties or headers
What is the purpose of calling next() in middleware?
ATo pass control to the next middleware
BTo end the request-response cycle
CTo restart the middleware stack
DTo send an error response
Explain how middleware functions work together in Express using req, res, and next.
Think about how a relay race passes the baton from one runner to the next.
You got /4 concepts.
    Describe what happens if a middleware neither calls next() nor sends a response.
    Imagine stopping in the middle of a line without telling anyone.
    You got /4 concepts.