HTTP caching headers help browsers and servers save time and data by reusing stored responses instead of downloading them again.
HTTP caching headers (ETag, Cache-Control) in Express
res.set('Cache-Control', 'public, max-age=3600'); res.set('ETag', 'unique-identifier');
Cache-Control tells browsers how long to keep the response (in seconds).
ETag is a unique ID for the response content to check if it changed.
res.set('Cache-Control', 'no-store');
res.set('Cache-Control', 'public, max-age=86400');
res.set('ETag', '12345abcde');
This Express server sends JSON data with Cache-Control and ETag headers.
If the client sends the same ETag in If-None-Match, the server replies with 304 Not Modified, saving bandwidth.
import express from 'express'; const app = express(); app.get('/data', (req, res) => { const data = { message: 'Hello, world!' }; const jsonData = JSON.stringify(data); const etag = 'W/"' + Buffer.from(jsonData).toString('base64') + '"'; res.set('Cache-Control', 'public, max-age=60'); res.set('ETag', etag); if (req.headers['if-none-match'] === etag) { res.status(304).end(); } else { res.json(data); } }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
ETags help browsers know if content changed without downloading it again.
Cache-Control max-age is in seconds and controls how long to keep cached data.
Always test caching behavior using browser DevTools Network tab to see headers and responses.
HTTP caching headers improve website speed and reduce data usage.
Use Cache-Control to set how long browsers keep content.
Use ETag to identify content versions and avoid unnecessary downloads.
Practice
ETag header in Express HTTP responses?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 CQuick Check:
ETag = version ID for caching [OK]
- Confusing ETag with Cache-Control duration
- Thinking ETag redirects clients
- Mixing ETag with content type headers
Solution
Step 1: Identify Express method to set headers
Express usesres.set()orres.header()to set headers. Both work, butres.set()is common and correct here.Step 2: Check Cache-Control value for 1 hour
1 hour = 3600 seconds, somax-age=3600is 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 AQuick Check:
Cache-Control max-age=3600 sets 1 hour caching [OK]
- Using wrong method like res.cache
- Confusing seconds with minutes in max-age
- Setting no-cache when caching is needed
If-None-Match matching the ETag?app.get('/data', (req, res) => {
const data = { message: 'Hello' };
const etag = '12345';
res.set('ETag', etag);
if (req.headers['if-none-match'] === etag) {
res.status(304).end();
} else {
res.json(data);
}
});Solution
Step 1: Check If-None-Match header against ETag
If the request'sIf-None-Matchheader equals the ETag '12345', the server responds with status 304 and no body.Step 2: Understand 304 response meaning
304 means Not Modified, telling the client to use cached data without sending the body again.Final Answer:
A 304 Not Modified status with no body -> Option BQuick Check:
If-None-Match matches ETag = 304 response [OK]
- Sending JSON body with 304 status
- Confusing 304 with 404 or 500 errors
- Ignoring If-None-Match header
app.get('/resource', (req, res) => {
const content = 'data';
res.setHeader('Cache-Control', 'max-age=600');
res.set('ETag', 'abc123');
res.send(content);
});Solution
Step 1: Check header setting methods
In Express,res.setHeaderandres.setboth 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 DQuick Check:
res.setHeader and res.set both valid; ETag string OK [OK]
- Thinking ETag must be numeric
- Confusing max-age units (seconds vs milliseconds)
- Assuming res.setHeader and res.set conflict
Cache-Control and ETag headers to achieve this?Solution
Step 1: Set Cache-Control for 5 minutes
5 minutes = 300 seconds, somax-age=300is correct to allow caching for that duration.Step 2: Include ETag header for validation
SettingETagallows clients to check if the cached version is still valid before using it.Step 3: Analyze other options
res.set('Cache-Control', 'no-store'); res.set('ETag', 'xyz789'); res.json(data); disables caching with 'no-store'. res.set('Cache-Control', 'max-age=300'); res.removeHeader('ETag'); res.json(data); removes ETag, losing validation. res.set('Cache-Control', 'max-age=300, no-cache'); res.set('ETag', 'xyz789'); res.json(data); uses conflicting directives 'max-age=300, no-cache' which disables caching despite max-age.Final Answer:
res.set({ 'Cache-Control': 'max-age=300', ETag: 'xyz789' }); res.json(data); -> Option AQuick Check:
Cache-Control max-age + ETag = proper caching [OK]
- Using no-store disables caching
- Removing ETag loses validation benefits
- Combining max-age with no-cache cancels caching
Ready to prove it?
Pass the test and this pattern moves unknown
