What is the purpose of conditional requests in Express?
Conditional requests help the server decide if it needs to send the full response or just a status like 304 Not Modified, saving bandwidth and improving speed.
Click to reveal answer
beginner
Which HTTP headers are commonly used for conditional requests?
The main headers are If-None-Match (for ETag) and If-Modified-Since (for last modified date). They tell the server what version the client has.
Click to reveal answer
intermediate
How does Express help handle conditional GET requests?
Express provides the res.sendFile() and res.send() methods that automatically check request headers and send a 304 status if the resource is unchanged.
Click to reveal answer
intermediate
What is an ETag and how is it used in Express conditional requests?
An ETag is a unique identifier for a resource version. Express can set ETags automatically, and clients send them back in If-None-Match headers to check if the resource changed.
Click to reveal answer
advanced
How can you manually handle conditional requests in Express?
You can check request headers like req.headers['if-none-match'] or req.headers['if-modified-since'], compare with your resource state, and respond with res.status(304).end() if unchanged.
Click to reveal answer
Which status code indicates the resource has NOT changed in a conditional request?
A200 OK
B500 Internal Server Error
C404 Not Found
D304 Not Modified
✗ Incorrect
304 Not Modified tells the client the cached resource is still valid, so no new data is sent.
Which header does the client send to check if the resource's ETag matches?
AIf-None-Match
BIf-Modified-Since
CETag
DContent-Type
✗ Incorrect
If-None-Match carries the ETag value from the client to check if the resource changed.
What does Express do when you use res.sendFile() with conditional headers?
AAlways sends full file content
BAutomatically sends 304 if resource is unchanged
CIgnores conditional headers
DThrows an error
✗ Incorrect
Express checks conditional headers and sends 304 Not Modified if the file hasn't changed.
Which header is used to check if a resource has been modified since a certain date?
ALast-Modified
BIf-None-Match
CIf-Modified-Since
DContent-Length
✗ Incorrect
If-Modified-Since lets the client ask if the resource changed after a specific date.
How can you respond with 304 Not Modified manually in Express?
Ares.status(304).end()
Bres.send(304)
Cres.json(304)
Dres.redirect(304)
✗ Incorrect
Use res.status(304).end() to send a 304 status with no body.
Explain how Express handles conditional GET requests using ETag and If-None-Match headers.
Think about how the server knows if the client has the latest version.
You got /5 concepts.
Describe how you would manually implement conditional request handling in an Express route.
Focus on checking headers and deciding the response status.
You got /4 concepts.
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
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 C
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
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 D
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
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 B
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
Step 1: Check usage of req.fresh
req.fresh is a property and must be accessed without parentheses: req.fresh
Step 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 A
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
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 A
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 unknownstrongwhile earning +1 Grip toward your certificate.