0
0
Node.jsframework~10 mins

HTTP caching headers in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - HTTP caching headers
Client sends HTTP request
Server receives request
Server checks cache headers
Has valid cache?
Send 304 Not Modified
Client uses cached
Display content
This flow shows how HTTP caching headers help the server decide whether to send a full response or a 304 status to use cached content.
Execution Sample
Node.js
const http = require('http');
http.createServer((req, res) => {
  res.setHeader('Cache-Control', 'max-age=10');
  res.end('Hello');
}).listen(3000);
A simple Node.js server sets a Cache-Control header to tell browsers to cache the response for 10 seconds.
Execution Table
StepRequest HeaderServer Cache CheckResponse Header SetResponse SentClient Action
1No If-None-Match or If-Modified-SinceNo cached version on serverCache-Control: max-age=10Full response with 'Hello'Cache response for 10 seconds
2Within 10 seconds, no revalidation headersCache still validCache-Control: max-age=10No new response sent (uses cache)Use cached content
3After 10 seconds, sends If-Modified-SinceCache expired, checks resourceCache-Control: max-age=10Sends full response againUpdate cache with new response
4After 10 seconds, sends If-None-Match with ETagETag matches current resourceCache-Control: max-age=10Sends 304 Not ModifiedUse cached content
5After 10 seconds, sends If-None-Match with different ETagETag does not matchCache-Control: max-age=10Sends full responseUpdate cache with new response
💡 Client stops requesting new data until cache expires or resource changes
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
Cache-Control headerundefinedmax-age=10max-age=10max-age=10max-age=10max-age=10
Client cache statusemptycached (valid)cached (valid)expiredexpiredexpired
ETag valueundefinedgeneratedgeneratedgeneratedgeneratedupdated or generated
Key Moments - 3 Insights
Why does the server sometimes send a 304 Not Modified instead of the full response?
When the client sends an If-None-Match or If-Modified-Since header and the resource has not changed, the server sends 304 to tell the client to use its cached copy, saving bandwidth. See execution_table rows 4 and 5.
What does the Cache-Control header 'max-age=10' mean?
It tells the client to consider the response fresh for 10 seconds. During this time, the client can use the cached response without asking the server again. See variable_tracker for Cache-Control header values.
What happens when the cache expires and the client sends a request with an outdated ETag?
The server detects the ETag does not match the current resource and sends the full updated response. The client then updates its cache. See execution_table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4. What response does the server send?
A304 Not Modified
BFull response with new content
C500 Internal Server Error
DEmpty response
💡 Hint
Check the 'Response Sent' column at Step 4 in the execution_table.
According to variable_tracker, what is the Cache-Control header value after Step 3?
Ano-cache
Bmax-age=5
Cmax-age=10
Dno-store
💡 Hint
Look at the 'Cache-Control header' row under 'After Step 3' in variable_tracker.
At which step does the client update its cache with a new response?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Check the 'Client Action' column in execution_table for when the cache is updated.
Concept Snapshot
HTTP caching headers help browsers store responses to avoid repeated downloads.
Cache-Control sets how long to keep data fresh (e.g., max-age=10 means 10 seconds).
ETag and Last-Modified headers let clients ask if content changed.
Server replies 304 Not Modified if content is unchanged, saving bandwidth.
Clients use cached data until it expires or changes.
This improves speed and reduces server load.
Full Transcript
HTTP caching headers control how browsers and servers share and reuse web data. When a client requests a page, the server can send headers like Cache-Control to tell the browser how long to keep the response. If the browser has a cached copy and it is still fresh, it uses that without asking the server. After the cache expires, the browser sends headers like If-None-Match with an ETag to check if the content changed. If not, the server replies with 304 Not Modified, telling the browser to keep using its cached copy. If the content changed, the server sends the full new response. This process saves time and bandwidth by avoiding unnecessary downloads. The example Node.js server sets Cache-Control to max-age=10, meaning the browser caches the response for 10 seconds before revalidating. This flow helps websites load faster and reduces server work.