0
0
Node.jsframework~20 mins

HTTP caching headers in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HTTP Caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the effect of setting Cache-Control: no-store?

In a Node.js HTTP server, if the response header Cache-Control is set to no-store, what will the browser do?

Node.js
res.setHeader('Cache-Control', 'no-store');
res.end('Hello');
AThe browser will cache the response only if the user is offline.
BThe browser will cache the response but revalidate it with the server before each use.
CThe browser will cache the response and use it without checking for updates.
DThe browser will not cache the response at all and will fetch it fresh every time.
Attempts:
2 left
💡 Hint

Think about what no-store means for caching.

📝 Syntax
intermediate
2:00remaining
Which code correctly sets a 1-hour cache expiration using Cache-Control?

Choose the Node.js code snippet that correctly sets the Cache-Control header to cache the response for 3600 seconds (1 hour).

Ares.setHeader('Cache-Control', 'max-age=3600');
Bres.setHeader('Cache-Control', 'maxage=3600');
Cres.setHeader('Cache-Control', 'max_age=3600');
Dres.setHeader('Cache-Control', 'max age=3600');
Attempts:
2 left
💡 Hint

Look for the exact syntax of the max-age directive.

🔧 Debug
advanced
2:00remaining
Why does this Node.js server response never cache despite max-age set?

Consider this Node.js code snippet:

res.setHeader('Cache-Control', 'max-age=3600');
res.setHeader('Pragma', 'no-cache');
res.end('Hello');

Why will the browser not cache this response?

Node.js
res.setHeader('Cache-Control', 'max-age=3600');
res.setHeader('Pragma', 'no-cache');
res.end('Hello');
ABecause the response body is too small to cache.
BBecause 'max-age' must be set to 0 to enable caching.
CBecause the 'Pragma: no-cache' header overrides 'Cache-Control' and disables caching.
DBecause 'Cache-Control' and 'Pragma' headers cannot be set together.
Attempts:
2 left
💡 Hint

Remember legacy headers can override modern ones.

state_output
advanced
2:00remaining
What is the value of the ETag header after this code runs?

Given this Node.js code snippet:

const crypto = require('crypto');
const body = 'Hello World';
const etag = crypto.createHash('md5').update(body).digest('hex');
res.setHeader('ETag', etag);
res.end(body);

What is the value of the ETag header?

Node.js
const crypto = require('crypto');
const body = 'Hello World';
const etag = crypto.createHash('md5').update(body).digest('hex');
res.setHeader('ETag', etag);
res.end(body);
A"etag-123456"
B"b10a8db164e0754105b7a99be72e3fe5"
C"md5:Hello World"
D"Hello World"
Attempts:
2 left
💡 Hint

MD5 hash of 'Hello World' is a known fixed value.

🧠 Conceptual
expert
2:00remaining
Which header combination enables client-side caching with validation?

To enable client-side caching where the browser stores the response but checks with the server if it is still valid before using it, which combination of headers should a Node.js server send?

ACache-Control: max-age=3600, ETag: "abc123"
BCache-Control: max-age=0, ETag: "abc123"
CCache-Control: max-age=3600, Pragma: no-cache
DCache-Control: no-store, Last-Modified: "Wed, 21 Oct 2015 07:28:00 GMT"
Attempts:
2 left
💡 Hint

Think about caching duration and validation tokens.