0
0
Expressframework~10 mins

HTTP caching headers (ETag, Cache-Control) 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 set a Cache-Control header that tells browsers to cache the response for 1 hour.

Express
app.get('/data', (req, res) => {
  res.set('[1]', 'public, max-age=3600');
  res.send({ message: 'Hello' });
});
Drag options to blanks, or click blank then click option'
AExpires
BContent-Type
CETag
DCache-Control
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Content-Type' instead of 'Cache-Control'.
Confusing 'ETag' with cache duration headers.
2fill in blank
medium

Complete the code to generate an ETag header using the express built-in method.

Express
app.get('/resource', (req, res) => {
  res.[1]();
  res.send('Resource content');
});
Drag options to blanks, or click blank then click option'
Aetag
BsetETag
CgenerateETag
DsetHeader
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use 'setHeader' directly without generating ETag.
Using non-existent methods like 'generateETag'.
3fill in blank
hard

Fix the error in the code to properly check the client's ETag and respond with 304 if it matches.

Express
app.get('/file', (req, res) => {
  const etag = '12345';
  if (req.headers['if-none-match'] === [1]) {
    res.status(304).end();
  } else {
    res.set('ETag', etag);
    res.send('File content');
  }
});
Drag options to blanks, or click blank then click option'
AETag
Betag
C'etag'
D'ETag'
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing to the string 'etag' instead of the variable etag.
Using 'ETag' which is not defined as a variable.
4fill in blank
hard

Fill both blanks to set Cache-Control to no-store and disable ETag generation.

Express
app.use((req, res, next) => {
  res.set('[1]', 'no-store');
  app.disable('[2]');
  next();
});
Drag options to blanks, or click blank then click option'
ACache-Control
Betag
CETag
DCache
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ETag' instead of 'etag' in app.disable.
Setting wrong header names like 'Cache'.
5fill in blank
hard

Fill both blanks to create a middleware that sets Cache-Control to private, sets ETag, and sends JSON response.

Express
app.get('/profile', (req, res) => {
  res.set('[1]', 'private, max-age=600');
  res.[2]();
  res.send({ user: 'Alice', id: 1, });
});
Drag options to blanks, or click blank then click option'
ACache-Control
Betag
C,
D;
Attempts:
3 left
💡 Hint
Common Mistakes
Using semicolon instead of comma in JSON object.
Forgetting to call etag() method.
Setting wrong header name.