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
✗ Incorrect
Express middleware functions always receive req, res, and next as parameters.
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
✗ Incorrect
Without calling next() or ending the response, the request will hang waiting.
Which middleware runs first in Express?
AThe last middleware added
BMiddleware added in random order
CMiddleware added first
DMiddleware with the shortest name
✗ Incorrect
Middleware executes in the order it is added to the app or router.
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
✗ Incorrect
Middleware can set headers or data on res before sending the response.
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
✗ Incorrect
Calling next() passes control to the next middleware function.
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.
Practice
(1/5)
1. What does the next() function do in Express middleware?
easy
A. It passes control to the next middleware or route handler.
B. It sends the response back to the client immediately.
C. It stops the middleware chain and ends the request.
D. It modifies the request object permanently.
Solution
Step 1: Understand middleware flow
Middleware functions receive req, res, and next. Calling next() tells Express to continue to the next middleware or route.
Step 2: Identify the role of next()
Calling next() does not send a response or stop the chain; it moves control forward.
Final Answer:
It passes control to the next middleware or route handler. -> Option A
Quick Check:
next() = pass control [OK]
Hint: Remember: next() moves to the next step in middleware [OK]
Common Mistakes:
Thinking next() sends a response
Assuming next() stops middleware chain
Confusing next() with modifying req or res
2. Which of the following is the correct way to define middleware in Express?
easy
A. app.use((req, res, next) => { next(); });
B. app.use((req, res) => { next(); });
C. app.use((req, res, next) => { res.next(); });
D. app.use((req, res, next) => { next; });
Solution
Step 1: Check middleware function signature
Express middleware must have three parameters: req, res, and next.
Step 2: Verify correct usage of next()
Calling next() as a function is required to pass control. app.use((req, res, next) => { next(); }); correctly uses next() with all parameters.
Final Answer:
app.use((req, res, next) => { next(); }); -> Option A
A. The second middleware is missing next parameter.
B. console.log() causes an error in middleware.
C. res.send() cannot be called in middleware.
D. The first middleware never calls next(), so request hangs.
Solution
Step 1: Check middleware flow control
The first middleware logs 'Start' but does not call next() or send a response, so Express waits indefinitely.
Step 2: Understand consequence of missing next()
Without calling next(), the second middleware never runs, and the client gets no response.
Final Answer:
The first middleware never calls next(), so request hangs. -> Option D
Quick Check:
Missing next() causes hang [OK]
Hint: Always call next() or send response to avoid hanging [OK]
Common Mistakes:
Ignoring missing next() causes no response
Thinking console.log() breaks middleware
Assuming res.send() is invalid in middleware
5. You want to create middleware that logs request method and URL, then passes control only if the method is GET. Otherwise, it should send a 405 status. Which code correctly implements this?
hard
A. app.use((req, res, next) => {
console.log(req.method, req.url);
next();
res.status(405).send('Method Not Allowed');
});
B. app.use((req, res, next) => {
console.log(req.method, req.url);
if (req.method === 'GET') res.send('OK');
else next();
});
C. app.use((req, res, next) => {
console.log(req.method, req.url);
if (req.method === 'GET') next();
else res.status(405).send('Method Not Allowed');
});
D. app.use((req, res) => {
console.log(req.method, req.url);
if (req.method !== 'GET') next();
else res.status(405).send('Method Not Allowed');
});
Solution
Step 1: Log method and URL
The middleware logs req.method and req.url correctly in all options.
Step 2: Control flow based on method
app.use((req, res, next) => {
console.log(req.method, req.url);
if (req.method === 'GET') next();
else res.status(405).send('Method Not Allowed');
}); calls next() only if method is GET, else sends 405 status. This matches the requirement.
Step 3: Identify incorrect options
app.use((req, res, next) => {
console.log(req.method, req.url);
if (req.method === 'GET') res.send('OK');
else next();
}); sends 'OK' on GET instead of passing control. app.use((req, res, next) => {
console.log(req.method, req.url);
next();
res.status(405).send('Method Not Allowed');
}); calls next() then tries to send response (invalid). app.use((req, res) => {
console.log(req.method, req.url);
if (req.method !== 'GET') next();
else res.status(405).send('Method Not Allowed');
}); lacks next parameter and reverses logic.
Final Answer:
app.use((req, res, next) => {
console.log(req.method, req.url);
if (req.method === 'GET') next();
else res.status(405).send('Method Not Allowed');
}); -> Option C
Quick Check:
next() only on GET, else 405 = app.use((req, res, next) => {
console.log(req.method, req.url);
if (req.method === 'GET') next();
else res.status(405).send('Method Not Allowed');
}); [OK]
Hint: Call next() only for GET; send 405 otherwise [OK]
Common Mistakes:
Calling next() then sending response
Swapping condition logic
Missing next parameter in middleware
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.