0
0
Expressframework~10 mins

Conditional requests handling in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Conditional requests handling
Client sends request with If-None-Match or If-Modified-Since header
Server checks resource's ETag or Last-Modified date
Resource not changed
Respond 304 Not Modified
Client receives response
The server checks if the client's cached version matches the current resource using headers, then decides to send the resource or a 304 status.
Execution Sample
Express
app.get('/data', (req, res) => {
  const etag = '12345';
  if (req.headers['if-none-match'] === etag) {
    res.status(304).end();
  } else {
    res.set('ETag', etag).send('Fresh data');
  }
});
This Express route sends a 304 status if the client's ETag matches, otherwise sends fresh data with an ETag.
Execution Table
StepRequest HeaderETag CheckActionResponse StatusResponse Body
1No If-None-Match headerNo matchSend data with ETag200"Fresh data"
2If-None-Match: 12345MatchSend 304 Not Modified304No body
3If-None-Match: 67890No matchSend data with ETag200"Fresh data"
4If-None-Match: 12345MatchSend 304 Not Modified304No body
💡 Requests end after sending either 304 or 200 response based on ETag match.
Variable Tracker
VariableStartRequest 1Request 2Request 3Request 4
req.headers['if-none-match']undefinedundefined"12345""67890""12345"
etag"12345""12345""12345""12345""12345"
ETag match?N/ANoYesNoYes
Response statusN/A200304200304
Key Moments - 2 Insights
Why does the server send a 304 status instead of the data sometimes?
When the client's If-None-Match header matches the server's ETag, the server knows the client has the latest data and sends 304 to avoid resending.
What happens if the client sends no If-None-Match header?
The server treats it as no match and sends the full data with a fresh ETag, as shown in step 1 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the response status at step 2 when If-None-Match is '12345'?
A200
B304
C404
D500
💡 Hint
Check row 2 under Response Status in execution_table.
At which request does the server send fresh data because the ETag does not match?
ARequest 2
BRequest 4
CRequest 3
DRequest 1
💡 Hint
Look at ETag match? and Response status columns in variable_tracker for requests.
If the server changes the ETag to '67890', what would happen when the client sends If-None-Match: '12345'?
AServer sends 200 with fresh data
BServer sends 304 Not Modified
CServer sends 404 Not Found
DServer ignores the header
💡 Hint
Compare the ETag with the If-None-Match header to decide response status.
Concept Snapshot
Conditional Requests Handling in Express:
- Client sends If-None-Match or If-Modified-Since headers.
- Server compares with current ETag or Last-Modified.
- If match, respond 304 Not Modified (no body).
- If no match, respond 200 with fresh data and ETag.
- Saves bandwidth by avoiding resending unchanged data.
Full Transcript
In Express, conditional requests help save bandwidth by letting the server check if the client's cached data is still fresh. The client sends headers like If-None-Match with an ETag value. The server compares this with its current ETag. If they match, the server sends a 304 Not Modified status without the data. If they don't match or the header is missing, the server sends the full data with a fresh ETag. This process repeats for each request, ensuring clients only download data when it changes.