Conditional requests help your server save work by sending data only if it has changed. This makes websites faster and uses less internet data.
Conditional requests handling in Express
app.get('/resource', (req, res) => { res.set('ETag', 'abc123'); if (req.fresh) { res.status(304).end(); } else { res.send('Your data here'); } });
req.fresh is true if the client's cached version is up to date.
Sending status 304 Not Modified tells the client to use its cached data.
app.get('/data', (req, res) => { res.set('ETag', '12345'); if (req.fresh) { res.status(304).end(); } else { res.send('Hello World'); } });
app.get('/info', (req, res) => { res.set('Last-Modified', new Date().toUTCString()); if (req.fresh) { res.status(304).end(); } else { res.send('Info data'); } });
This server sends a message with an ETag header. If the client sends the same ETag back, the server replies with 304 Not Modified, saving bandwidth.
import express from 'express'; const app = express(); app.get('/message', (req, res) => { const message = 'Hello from server!'; const etag = 'v1'; res.set('ETag', etag); if (req.headers['if-none-match'] === etag) { res.status(304).end(); } else { res.send(message); } }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Always set ETag or Last-Modified headers to enable conditional requests.
Use req.fresh or check if-none-match and if-modified-since headers to decide if data changed.
Responding with 304 means no body is sent, so the client uses cached data.
Conditional requests help reduce data sent by checking if client cache is fresh.
Use ETag or Last-Modified headers to support conditional requests.
Respond with 304 status to tell client to use cached data.
Practice
Solution
Step 1: Understand conditional requests concept
Conditional requests check if the client already has fresh data to avoid sending it again.Step 2: Identify the purpose in Express
Express uses conditional requests to reduce unnecessary data transfer by responding with 304 if data is fresh.Final Answer:
To reduce data sent by checking if the client cache is fresh -> Option CQuick Check:
Conditional requests reduce data transfer = B [OK]
- Thinking conditional requests increase server load
- Assuming full data is always sent
- Believing caching is disabled by conditional requests
Solution
Step 1: Recall Express request object properties
The freshness of a request is checked on the request object, not the response.Step 2: Identify the correct property name
Express provides req.fresh to check if the request is fresh based on headers.Final Answer:
req.fresh -> Option DQuick Check:
Check freshness with req.fresh = B [OK]
- Using res.isFresh() instead of req.fresh
- Confusing property names like req.isFresh() which don't exist
- Trying to check freshness on response object
app.get('/data', (req, res) => {
res.set('ETag', '12345');
if (req.fresh) {
res.status(304).end();
} else {
res.send('Full data response');
}
});Solution
Step 1: Understand ETag and req.fresh
The server sets ETag '12345'. If the client sends the same ETag, req.fresh returns true.Step 2: Analyze the conditional response
If req.fresh is true, the server responds with status 304 and ends response without body.Final Answer:
Status 304 with no body -> Option BQuick Check:
Matching ETag triggers 304 no body = A [OK]
- Sending full data even when ETag matches
- Returning status 200 with empty body
- Confusing 304 with error status
app.get('/info', (req, res) => {
res.set('Last-Modified', new Date().toUTCString());
if (req.fresh()) {
res.status(304).end();
} else {
res.send('Info data');
}
});Solution
Step 1: Check usage of req.fresh
req.fresh is a property and must be accessed without parentheses: req.freshStep 2: Verify other parts
Last-Modified header is set correctly, res.send() is called in else branch, arrow function syntax is correct.Final Answer:
Calling req.fresh() with parentheses (it's a property) -> Option AQuick Check:
Property access needs no () = A [OK]
- Treating req.fresh as a method by adding parentheses
- Misformatting Last-Modified header
- Not sending response in all branches
Solution
Step 1: Understand headers and freshness check
ETag and Last-Modified headers must be set for conditional requests to work properly.Step 2: Use req.fresh to decide response
req.fresh checks these headers and client cache; if fresh, respond 304, else send full data.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.Final Answer:
Set ETag and Last-Modified headers, then check req.fresh to send 304 or full data -> Option AQuick Check:
Set headers + check req.fresh = D [OK]
- Not setting headers but checking freshness
- Setting headers but ignoring req.fresh
- Using req.fresh without headers
Ready to prove it?
Pass the test and this pattern moves unknown
