The ETag header provides a unique identifier for a specific version of a resource. It helps browsers and servers check if the resource has changed, so the browser can use a cached version if it hasn't.
Click to reveal answer
beginner
Explain the role of the Cache-Control header in HTTP caching.
The Cache-Control header tells browsers and proxies how to store and reuse responses. It can specify how long to keep a resource, whether to revalidate it, or if it should not be cached at all.
Click to reveal answer
intermediate
How does Express help you set ETag headers automatically?
Express automatically generates ETag headers for responses by default, based on the response body. This helps with efficient caching without extra code.
Click to reveal answer
beginner
What does the Cache-Control: no-store directive do?
It tells browsers and proxies not to store any part of the response. This means the resource will always be fetched fresh from the server.
Click to reveal answer
beginner
Why is it important to use caching headers like ETag and Cache-Control in web applications?
Using caching headers improves performance by reducing unnecessary data transfer. It saves bandwidth, speeds up page loads, and reduces server load by letting browsers reuse cached resources when possible.
Click to reveal answer
What does the ETag header represent?
AThe size of the resource in bytes
BA unique version identifier for a resource
CThe expiration time of a cached resource
DThe server's IP address
✗ Incorrect
ETag is a unique identifier for a resource version, used to check if the cached copy is still valid.
Which Cache-Control directive tells the browser to always fetch a fresh copy?
Ano-cache
Bmax-age=3600
Cpublic
Dprivate
✗ Incorrect
no-cache means the browser must revalidate with the server before using the cached resource.
In Express, how can you disable automatic ETag generation?
Aapp.etag = false
Bapp.disable('etag')
Capp.use(etag(false))
Dapp.set('etag', false)
✗ Incorrect
Use app.set('etag', false) to turn off automatic ETag generation in Express.
What does Cache-Control: max-age=0 mean?
ACache forever
BDo not cache at all
CResource is stale immediately and must be revalidated
DCache for 1 hour
✗ Incorrect
max-age=0 means the cached resource is immediately stale and requires revalidation.
Which header does the browser send to check if the cached resource matches the server's version?
AIf-None-Match
BETag
CCache-Control
DIf-Modified-Since
✗ Incorrect
The browser sends If-None-Match with the ETag value to ask if the resource has changed.
Describe how ETag and Cache-Control headers work together to improve web performance.
Think about how the browser knows when to reuse cached data or ask the server for updates.
You got /4 concepts.
Explain how to set caching headers in an Express app to cache a response for 1 hour.
Remember Cache-Control max-age is in seconds.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the ETag header in Express HTTP responses?
easy
A. To redirect the client to a different URL
B. To specify how long the browser should cache the resource
C. To identify a specific version of a resource for caching validation
D. To indicate the content type of the response
Solution
Step 1: Understand the role of ETag
The ETag header provides a unique identifier for a specific version of a resource, allowing the server to tell if the resource has changed.
Step 2: Compare with other headers
Cache-Control sets caching duration, redirects use Location header, and content type is set by Content-Type header, so these are not related to ETag.
Final Answer:
To identify a specific version of a resource for caching validation -> Option C
Quick Check:
ETag = version ID for caching [OK]
Hint: ETag tags resource versions to avoid unnecessary downloads [OK]
Common Mistakes:
Confusing ETag with Cache-Control duration
Thinking ETag redirects clients
Mixing ETag with content type headers
2. Which of the following is the correct way to set a Cache-Control header in Express to cache a response for 1 hour?
easy
A. res.set('Cache-Control', 'max-age=3600')
B. res.header('Cache-Control', 'no-cache')
C. res.cache('max-age=3600')
D. res.setHeader('Cache-Control', 'max-age=60')
Solution
Step 1: Identify Express method to set headers
Express uses res.set() or res.header() to set headers. Both work, but res.set() is common and correct here.
Step 2: Check Cache-Control value for 1 hour
1 hour = 3600 seconds, so max-age=3600 is correct. res.header('Cache-Control', 'no-cache') uses 'no-cache' which disables caching. res.cache('max-age=3600') is invalid method. res.setHeader('Cache-Control', 'max-age=60') sets max-age to 60 seconds, not 1 hour.
Final Answer:
res.set('Cache-Control', 'max-age=3600') -> Option A
A. Using both res.setHeader and res.set causes header conflicts
B. ETag header value must be a number, not a string
C. Cache-Control max-age should be in milliseconds, not seconds
D. No bug; code sets headers correctly
Solution
Step 1: Check header setting methods
In Express, res.setHeader and res.set both set headers correctly; using both is allowed and does not conflict.
Step 2: Validate header values
ETag can be any string value. Cache-Control max-age is in seconds, so '600' is valid for 10 minutes.
Final Answer:
No bug; code sets headers correctly -> Option D
Quick Check:
res.setHeader and res.set both valid; ETag string OK [OK]
Hint: res.set and res.setHeader both valid for headers [OK]
Common Mistakes:
Thinking ETag must be numeric
Confusing max-age units (seconds vs milliseconds)
Assuming res.setHeader and res.set conflict
5. You want to implement HTTP caching in Express so that clients cache a JSON response for 5 minutes, but also use ETag to validate changes. Which code snippet correctly combines Cache-Control and ETag headers to achieve this?
hard
A. res.set({ 'Cache-Control': 'max-age=300', ETag: 'xyz789' }); res.json(data);
B. res.set('Cache-Control', 'no-store'); res.set('ETag', 'xyz789'); res.json(data);
C. res.set('Cache-Control', 'max-age=300'); res.removeHeader('ETag'); res.json(data);
D. res.set('Cache-Control', 'max-age=300, no-cache'); res.set('ETag', 'xyz789'); res.json(data);
Solution
Step 1: Set Cache-Control for 5 minutes
5 minutes = 300 seconds, so max-age=300 is correct to allow caching for that duration.
Step 2: Include ETag header for validation
Setting ETag allows clients to check if the cached version is still valid before using it.