cacheData is defined as { message: 'Hello' }, so the if condition is true.
Step 2: Determine middleware behavior
Since cacheData exists, middleware sends JSON response and does not call next().
Final Answer:
{"message":"Hello"} -> Option A
Quick Check:
cacheData present sends cached response [OK]
Hint: If cacheData exists, middleware sends it and skips next() [OK]
Common Mistakes:
Assuming next() runs even if cacheData exists
Confusing JSON string with object
Expecting original handler response
4. Identify the bug in this cache middleware code:
let cacheData;
function cache(req, res, next) {
if (cacheData) {
res.send(cacheData);
}
next();
}
medium
A. Middleware function missing req parameter
B. cacheData is not initialized
C. res.send should be res.json
D. next() is called even after sending a response
Solution
Step 1: Analyze control flow
If cacheData exists, res.send(cacheData) sends a response, but next() is still called afterward.
Step 2: Understand Express behavior
Calling next() after sending a response can cause errors or duplicate responses.
Final Answer:
next() is called even after sending a response -> Option D
Quick Check:
Calling next() after res.send causes errors [OK]
Hint: Do not call next() after sending a response [OK]
Common Mistakes:
Calling next() unconditionally after res.send
Ignoring middleware flow control
Assuming res.send and next() can run together
5. You want to implement cache middleware that stores responses for 10 seconds and then expires. Which approach correctly achieves this in Express?
hard
A. Store response in cacheData with a timestamp; clear cacheData if older than 10 seconds before sending
B. Use setTimeout to clear cacheData every 10 seconds regardless of requests
C. Send cached response only if cacheData exists, without checking time
D. Clear cacheData manually by restarting the server every 10 seconds
Solution
Step 1: Understand cache expiration logic
Cache should store data with a timestamp and check age before sending cached response.
Step 2: Evaluate options
Store response in cacheData with a timestamp; clear cacheData if older than 10 seconds before sending checks timestamp and clears stale cache before responding, ensuring fresh data.
Final Answer:
Store response in cacheData with a timestamp; clear cacheData if older than 10 seconds before sending -> Option A
Quick Check:
Cache expiration needs timestamp check [OK]
Hint: Use timestamps to expire cache, not just timers or manual clears [OK]
Common Mistakes:
Ignoring cache expiration timing
Clearing cache blindly without request context
Relying on server restart to clear cache
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.