0
0
Expressframework~10 mins

Cache middleware pattern in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a cache object in Express middleware.

Express
const cache = [1]();
Drag options to blanks, or click blank then click option'
Anew Array
Bnew Set
Cnew Object
Dnew Map
Attempts:
3 left
💡 Hint
Common Mistakes
Using new Set instead of Map, which only stores unique values without keys.
Using new Array which is not suitable for key-value caching.
2fill in blank
medium

Complete the middleware function to check if the cache has the requested URL.

Express
function cacheMiddleware(req, res, next) {
  if (cache.[1](req.url)) {
    res.send(cache.get(req.url));
  } else {
    next();
  }
}
Drag options to blanks, or click blank then click option'
Ahas
Bincludes
Ccontains
Dexists
Attempts:
3 left
💡 Hint
Common Mistakes
Using includes or contains which are not methods of Map.
Using exists which is not a JavaScript method.
3fill in blank
hard

Fix the error in the middleware to store the response body in cache after sending it.

Express
function cacheMiddleware(req, res, next) {
  if (cache.has(req.url)) {
    res.send(cache.get(req.url));
  } else {
    const originalSend = res.send;
    res.send = function(body) {
      cache.set(req.url, [1]);
      originalSend.call(this, body);
    };
    next();
  }
}
Drag options to blanks, or click blank then click option'
Ares.body
Bthis.body
Cbody
Dreq.body
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access res.body which is undefined.
Using req.body which is the request data, not response.
4fill in blank
hard

Fill both blanks to create a cache middleware that stores JSON responses.

Express
function cacheMiddleware(req, res, next) {
  if (cache.has(req.url)) {
    res.json(cache.get(req.url));
  } else {
    const originalJson = res.json;
    res.json = function([1]) {
      cache.set(req.url, [2]);
      originalJson.call(this, [1]);
    };
    next();
  }
}
Drag options to blanks, or click blank then click option'
Adata
Bres.body
Dbody
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the parameter and cache value.
Trying to use res.body which is not defined.
5fill in blank
hard

Fill all three blanks to implement cache middleware with expiration time.

Express
const cache = new Map();
const cacheExpiry = new Map();
const CACHE_DURATION = 60000; // 1 minute

function cacheMiddleware(req, res, next) {
  const now = Date.now();
  if (cache.has(req.url) && cacheExpiry.get(req.url) > [1]) {
    res.send(cache.get(req.url));
  } else {
    const originalSend = res.send;
    res.send = function([2]) {
      cache.set(req.url, [3]);
      cacheExpiry.set(req.url, Date.now() + CACHE_DURATION);
      originalSend.call(this, [2]);
    };
    next();
  }
}
Drag options to blanks, or click blank then click option'
Anow
Bbody
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variables for time comparison.
Mixing up parameter names for the response body.