Consider this Express cache middleware snippet. What happens when a cached response is found?
const cache = {};
function cacheMiddleware(req, res, next) {
const key = req.originalUrl;
if (cache[key]) {
res.send(cache[key]);
} else {
res.sendResponse = res.send;
res.send = (body) => {
cache[key] = body;
res.sendResponse(body);
};
next();
}
}Think about what happens when cache[key] exists and how res.send is used.
If a cached response exists, the middleware sends it immediately using res.send and does not call next(), so the request does not proceed further.
Choose the correct way to cache the response body inside Express middleware.
const cache = {};
function cacheMiddleware(req, res, next) {
const key = req.originalUrl;
if (cache[key]) {
res.send(cache[key]);
} else {
res.sendResponse = res.send;
res.send = (body) => {
// Cache the body here
res.sendResponse(body);
};
next();
}
}Remember how to assign a value to an object property using a variable key.
To store the response body in the cache object using the key variable, use cache[key] = body; This assigns the value correctly.
Identify the bug in this cache middleware that causes the client to hang without receiving a response.
const cache = {};
function cacheMiddleware(req, res, next) {
const key = req.originalUrl;
if (cache[key]) {
res.send(cache[key]);
} else {
res.send = (body) => {
cache[key] = body;
res.send(body);
};
next();
}
}Look at how res.send is redefined and what happens when it calls res.send inside itself.
Overwriting res.send and then calling res.send inside the new function causes infinite recursion. This leads to a stack overflow and the response never sends.
Given this cache middleware and two requests to '/data', what is the cache content after both requests?
const cache = {};
function cacheMiddleware(req, res, next) {
const key = req.originalUrl;
if (cache[key]) {
res.send(cache[key]);
} else {
res.sendResponse = res.send;
res.send = (body) => {
cache[key] = body;
res.sendResponse(body);
};
next();
}
}
// Simulated requests:
// 1st request to '/data' sends 'First response'
// 2nd request to '/data' sends 'Second response'
// Assume middleware is used correctly and responses are sent accordingly.Think about when the cache is updated and when the cached response is sent.
The first request caches 'First response'. The second request finds the cache and sends the cached 'First response' without updating it. So cache remains with the first response.
Consider this cache middleware pattern that stores responses in memory keyed by URL. What is a key limitation of this approach in a real-world Express app?
const cache = {};
function cacheMiddleware(req, res, next) {
const key = req.originalUrl;
if (cache[key]) {
res.send(cache[key]);
} else {
res.sendResponse = res.send;
res.send = (body) => {
cache[key] = body;
res.sendResponse(body);
};
next();
}
}Think about what happens if the data changes but the cache never updates or clears.
This simple cache stores data indefinitely in memory without any expiration or invalidation logic. This can cause the app to serve outdated responses if the underlying data changes.