0
0
Expressframework~20 mins

Cache middleware pattern in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this cache middleware do when a cached response exists?

Consider this Express cache middleware snippet. What happens when a cached response is found?

Express
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();
  }
}
AIt throws an error because res.send is overwritten incorrectly.
BIt sends the cached response immediately without calling next().
CIt clears the cache and then calls next().
DIt calls next() and then sends the cached response.
Attempts:
2 left
💡 Hint

Think about what happens when cache[key] exists and how res.send is used.

📝 Syntax
intermediate
2:00remaining
Which option correctly caches the response body in Express middleware?

Choose the correct way to cache the response body inside Express middleware.

Express
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();
  }
}
Acache[key] = body;
Bcache[key] == body;
Ccache[key] === body;
Dcache.key = body;
Attempts:
2 left
💡 Hint

Remember how to assign a value to an object property using a variable key.

🔧 Debug
advanced
2:00remaining
Why does this cache middleware cause the response to never send?

Identify the bug in this cache middleware that causes the client to hang without receiving a response.

Express
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();
  }
}
Ares.send is overwritten and calls itself recursively causing a stack overflow.
Bcache[key] is never assigned so cache is always empty.
Cnext() is not called so the request hangs.
Dres.sendResponse is missing causing a syntax error.
Attempts:
2 left
💡 Hint

Look at how res.send is redefined and what happens when it calls res.send inside itself.

state_output
advanced
2:00remaining
What is the cache content after these requests?

Given this cache middleware and two requests to '/data', what is the cache content after both requests?

Express
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.
A{}
B{"/data": "Second response"}
C{"/data": "First response"}
D{"/data": null}
Attempts:
2 left
💡 Hint

Think about when the cache is updated and when the cached response is sent.

🧠 Conceptual
expert
3:00remaining
What is a key limitation of this simple cache middleware pattern?

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?

Express
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();
  }
}
AIt encrypts cached data to protect user privacy.
BIt automatically scales across multiple server instances without extra configuration.
CIt supports caching POST requests with different bodies automatically.
DIt does not handle cache invalidation or expiration, so stale data may be served indefinitely.
Attempts:
2 left
💡 Hint

Think about what happens if the data changes but the cache never updates or clears.