0
0
Node.jsframework~10 mins

ETag and conditional requests in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ETag and conditional requests
Client sends GET request
Server generates ETag for resource
Server checks If-None-Match header
ETag matches
Respond 304
Client uses cached resource
The client requests a resource, the server creates an ETag, compares it with the client's If-None-Match header, and responds with 304 if unchanged or 200 with data if changed.
Execution Sample
Node.js
const etag = generateETag(resource);
if (req.headers['if-none-match'] === etag) {
  res.status(304).end();
} else {
  res.setHeader('ETag', etag);
  res.status(200).send(resource);
}
Server checks client's ETag and responds with 304 if resource unchanged, else sends resource with new ETag.
Execution Table
StepClient RequestServer ETagIf-None-Match HeaderConditionServer ResponseClient Action
1GET /resource"abc123"NoneNo If-None-Match header200 + resource + ETag "abc123"Cache resource with ETag "abc123"
2GET /resource"abc123""abc123"ETag matches304 Not ModifiedUse cached resource
3GET /resource"def456""abc123"ETag differs200 + resource + ETag "def456"Update cache with new resource and ETag
4GET /resource"def456""def456"ETag matches304 Not ModifiedUse cached resource
💡 Requests stop when client uses cached resource or receives updated resource.
Variable Tracker
VariableStartAfter 1After 2After 3After 4
etagundefined"abc123""abc123""def456""def456"
req.headers['if-none-match']undefinedundefined"abc123""abc123""def456"
res.statusCodeundefined200304200304
Key Moments - 3 Insights
Why does the server respond with 304 instead of 200 sometimes?
When the client's If-None-Match header matches the server's current ETag (see execution_table rows 2 and 4), the server knows the client has the latest version and sends 304 to avoid resending data.
What happens if the client sends no If-None-Match header?
The server treats it as a new request and sends the full resource with a 200 status and an ETag header (see execution_table row 1).
How does the client know to update its cache?
When the server responds with 200 and a new ETag different from the cached one (see execution_table row 3), the client replaces its cached resource and ETag.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the server response at step 2?
A200 with resource and ETag
B404 Not Found
C304 Not Modified
D500 Internal Server Error
💡 Hint
Check the 'Server Response' column at step 2 in the execution_table.
At which step does the server send a new ETag "def456"?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Server ETag' and 'Server Response' columns in the execution_table.
If the client never sends an If-None-Match header, what will the server response be at first request?
A200 with resource and ETag
B304 Not Modified
C400 Bad Request
D503 Service Unavailable
💡 Hint
Refer to step 1 in the execution_table where If-None-Match is None.
Concept Snapshot
ETag and Conditional Requests:
- Server generates an ETag for a resource.
- Client sends If-None-Match header with cached ETag.
- If ETag matches, server replies 304 Not Modified (no body).
- If ETag differs or missing, server sends 200 with resource and new ETag.
- Saves bandwidth and speeds up loading by avoiding unnecessary data transfer.
Full Transcript
This visual trace shows how ETag and conditional requests work in Node.js. The client requests a resource. The server creates an ETag string representing the resource version. The client sends this ETag back in the If-None-Match header on subsequent requests. The server compares the client's ETag with the current one. If they match, the server responds with 304 Not Modified, telling the client to use its cached copy. If they differ or the client sends no ETag, the server sends the full resource with a 200 status and the new ETag. This process saves bandwidth and improves performance by avoiding sending unchanged data.