This example shows two middleware functions running in order. The first logs the request method and URL. The second adds a greeting message to the request. Finally, the route handler sends that greeting as the response.
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.