Bird
Raised Fist0
Expressframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of handling conditional requests in Express?
easy
A. To increase server processing time
B. To always send full response data regardless of cache
C. To reduce data sent by checking if the client cache is fresh
D. To disable client caching completely

Solution

  1. Step 1: Understand conditional requests concept

    Conditional requests check if the client already has fresh data to avoid sending it again.
  2. Step 2: Identify the purpose in Express

    Express uses conditional requests to reduce unnecessary data transfer by responding with 304 if data is fresh.
  3. Final Answer:

    To reduce data sent by checking if the client cache is fresh -> Option C
  4. Quick Check:

    Conditional requests reduce data transfer = B [OK]
Hint: Conditional requests save bandwidth by checking cache freshness [OK]
Common Mistakes:
  • Thinking conditional requests increase server load
  • Assuming full data is always sent
  • Believing caching is disabled by conditional requests
2. Which Express property is used to check if the request is fresh based on ETag or Last-Modified headers?
easy
A. res.isFresh()
B. res.checkFresh()
C. req.isFresh()
D. req.fresh

Solution

  1. Step 1: Recall Express request object properties

    The freshness of a request is checked on the request object, not the response.
  2. Step 2: Identify the correct property name

    Express provides req.fresh to check if the request is fresh based on headers.
  3. Final Answer:

    req.fresh -> Option D
  4. Quick Check:

    Check freshness with req.fresh = B [OK]
Hint: Use req.fresh to check cache freshness in Express [OK]
Common Mistakes:
  • Using res.isFresh() instead of req.fresh
  • Confusing property names like req.isFresh() which don't exist
  • Trying to check freshness on response object
3. What will the following Express code send if the client sends a matching ETag header?
app.get('/data', (req, res) => {
  res.set('ETag', '12345');
  if (req.fresh) {
    res.status(304).end();
  } else {
    res.send('Full data response');
  }
});
medium
A. Full data response with status 200
B. Status 304 with no body
C. Status 500 error
D. Empty response with status 200

Solution

  1. Step 1: Understand ETag and req.fresh

    The server sets ETag '12345'. If the client sends the same ETag, req.fresh returns true.
  2. Step 2: Analyze the conditional response

    If req.fresh is true, the server responds with status 304 and ends response without body.
  3. Final Answer:

    Status 304 with no body -> Option B
  4. Quick Check:

    Matching ETag triggers 304 no body = A [OK]
Hint: Matching ETag means send 304 status with empty body [OK]
Common Mistakes:
  • Sending full data even when ETag matches
  • Returning status 200 with empty body
  • Confusing 304 with error status
4. Identify the error in this Express route handling conditional requests:
app.get('/info', (req, res) => {
  res.set('Last-Modified', new Date().toUTCString());
  if (req.fresh()) {
    res.status(304).end();
  } else {
    res.send('Info data');
  }
});
medium
A. Calling req.fresh() with parentheses (it's a property)
B. Setting Last-Modified header incorrectly
C. Missing res.send() in both branches
D. Using arrow function incorrectly

Solution

  1. Step 1: Check usage of req.fresh

    req.fresh is a property and must be accessed without parentheses: req.fresh
  2. Step 2: Verify other parts

    Last-Modified header is set correctly, res.send() is called in else branch, arrow function syntax is correct.
  3. Final Answer:

    Calling req.fresh() with parentheses (it's a property) -> Option A
  4. Quick Check:

    Property access needs no () = A [OK]
Hint: Always access req.fresh without parentheses [OK]
Common Mistakes:
  • Treating req.fresh as a method by adding parentheses
  • Misformatting Last-Modified header
  • Not sending response in all branches
5. You want to optimize an Express API to support conditional requests using both ETag and Last-Modified headers. Which approach correctly implements this to respond with 304 when appropriate? A) Set ETag and Last-Modified headers, then check req.fresh to send 304 or full data. B) Only set ETag header and always send full data. C) Set Last-Modified header but never check req.fresh, always send 200. D) Use req.fresh without setting any headers.
hard
A. Set ETag and Last-Modified headers, then check req.fresh to send 304 or full data
B. Only set ETag header and always send full data
C. Set Last-Modified header but never check req.fresh, always send 200
D. Use req.fresh without setting any headers

Solution

  1. Step 1: Understand headers and freshness check

    ETag and Last-Modified headers must be set for conditional requests to work properly.
  2. Step 2: Use req.fresh to decide response

    req.fresh checks these headers and client cache; if fresh, respond 304, else send full data.
  3. Step 3: Evaluate options

    Only Set ETag and Last-Modified headers, then check req.fresh to send 304 or full data: Set ETag and Last-Modified headers, then check req.fresh to send 304 or full data correctly sets headers and uses req.fresh to conditionally send 304 or data.
  4. Final Answer:

    Set ETag and Last-Modified headers, then check req.fresh to send 304 or full data -> Option A
  5. Quick Check:

    Set headers + check req.fresh = D [OK]
Hint: Set headers and check req.fresh to send 304 or data [OK]
Common Mistakes:
  • Not setting headers but checking freshness
  • Setting headers but ignoring req.fresh
  • Using req.fresh without headers
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard