Bird
Raised Fist0
Node.jsframework~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Cache-Control: max-age=3600 header instruct the browser to do?
easy
A. Cache the response for 3600 seconds before checking for updates
B. Never cache the response
C. Cache the response indefinitely
D. Only cache the response if the user is offline

Solution

  1. Step 1: Understand the max-age directive meaning

    The max-age directive tells the browser how many seconds it can keep the cached response before it must revalidate.
  2. Step 2: Interpret max-age=3600

    max-age=3600 means the browser should cache the response for 3600 seconds (1 hour) before checking for a new version.
  3. Final Answer:

    Cache the response for 3600 seconds before checking for updates -> Option A
  4. Quick Check:

    max-age=3600 = Cache 1 hour [OK]
Hint: max-age sets cache time in seconds [OK]
Common Mistakes:
  • Confusing max-age with no-cache
  • Thinking max-age means no caching
  • Assuming max-age caches forever
2. Which of the following is the correct way to set a no-store cache control header in a Node.js HTTP response?
easy
A. res.header('Cache-Control', 'no-store')
B. res.setHeader('Cache-Control', 'no-store')
C. res.set('Cache-Control', 'no-store')
D. res.cacheControl = 'no-store'

Solution

  1. Step 1: Recall Node.js HTTP response header syntax

    In Node.js, the method to set headers is res.setHeader(name, value).
  2. Step 2: Match the correct method and header

    res.setHeader('Cache-Control', 'no-store') uses res.setHeader('Cache-Control', 'no-store'), which is the correct syntax to set the no-store directive.
  3. Final Answer:

    res.setHeader('Cache-Control', 'no-store') -> Option B
  4. Quick Check:

    Use res.setHeader() to set headers [OK]
Hint: Use res.setHeader() to set HTTP headers [OK]
Common Mistakes:
  • Using incorrect method names like res.set or res.header
  • Assigning header as a property instead of using setHeader
  • Confusing Express methods with Node.js core methods
3. Consider this Node.js code snippet setting caching headers:
res.setHeader('Cache-Control', 'no-cache, max-age=0');
res.end('Hello');
What will the browser do with this response?
medium
A. Ignore caching and always fetch fresh data
B. Cache the response for 0 seconds and never revalidate
C. Cache the response indefinitely without revalidation
D. Cache the response for 0 seconds and revalidate before each use

Solution

  1. Step 1: Understand the meaning of no-cache and max-age=0

    The no-cache directive means the browser must revalidate with the server before using the cached response. max-age=0 means the cached response is immediately stale.
  2. Step 2: Combine the effects

    With no-cache and max-age=0, the browser caches the response but must check with the server before each use, effectively caching for 0 seconds without skipping validation.
  3. Final Answer:

    Cache the response for 0 seconds and revalidate before each use -> Option D
  4. Quick Check:

    no-cache + max-age=0 = revalidate every time [OK]
Hint: no-cache means revalidate before use [OK]
Common Mistakes:
  • Thinking no-cache means no caching at all
  • Ignoring max-age=0 effect
  • Assuming browser ignores these headers
4. You wrote this Node.js code to prevent caching:
res.end('Data');
res.setHeader('Cache-Control', 'no-cache, no-store');
But the browser still caches the response. What is the likely mistake?
medium
A. The 'no-store' directive is misspelled
B. The response status code is missing
C. Headers are set after sending the response body
D. The Cache-Control header should be lowercase

Solution

  1. Step 1: Check header setting timing

    Headers must be set before sending the response body. If headers are set after calling res.end(), they won't be sent.
  2. Step 2: Confirm header spelling and case

    Cache-Control is case-insensitive and 'no-store' is spelled correctly, so these are not issues.
  3. Step 3: Status code is optional for caching headers

    Missing status code does not cause caching issues here.
  4. Final Answer:

    Headers are set after sending the response body -> Option C
  5. Quick Check:

    Set headers before res.end() [OK]
Hint: Set headers before sending response body [OK]
Common Mistakes:
  • Setting headers after res.end()
  • Misspelling cache directives
  • Assuming header case matters
5. You want to serve a JSON API response that should never be cached by browsers or proxies. Which combination of HTTP caching headers in Node.js ensures this behavior?
hard
A. res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate')
B. res.setHeader('Cache-Control', 'max-age=3600')
C. res.setHeader('Cache-Control', 'public, max-age=86400')
D. res.setHeader('Cache-Control', 'private, max-age=0')

Solution

  1. Step 1: Understand no-store and no-cache directives

    no-store prevents any caching anywhere. no-cache forces revalidation. must-revalidate requires caches to obey freshness rules strictly.
  2. Step 2: Analyze each option

    res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate') combines all directives to prevent caching fully. res.setHeader('Cache-Control', 'max-age=3600') and C allow caching. res.setHeader('Cache-Control', 'private, max-age=0') allows private caching but with max-age=0, which still permits revalidation.
  3. Final Answer:

    res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate') -> Option A
  4. Quick Check:

    no-store + no-cache = no caching [OK]
Hint: Use no-store to fully prevent caching [OK]
Common Mistakes:
  • Using max-age allowing caching
  • Confusing private with no-store
  • Omitting must-revalidate for strictness
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard