Bird
Raised Fist0
Node.jsframework~20 mins

ETag and conditional requests in Node.js - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ETag Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What is the primary purpose of an ETag in HTTP?
ETags are used in HTTP to help with which of the following?
ATo uniquely identify a specific version of a resource for caching and conditional requests
BTo specify the language preference of the client
CTo encrypt the content of the HTTP response for security
DTo indicate the server's processing time for the request
Attempts:
2 left
💡 Hint
Think about how browsers avoid downloading unchanged files.
❓ component_behavior
intermediate
1:30remaining
What happens when a client sends an If-None-Match header with a matching ETag?
In a Node.js server using ETags, if the client sends an If-None-Match header with an ETag that matches the current resource's ETag, what should the server respond with?
ARespond with status 500 Internal Server Error
BRespond with status 200 OK and the full resource body
CRespond with status 404 Not Found
DRespond with status 304 Not Modified and no response body
Attempts:
2 left
💡 Hint
Consider what happens when the resource has not changed.
📝 Syntax
advanced
2:00remaining
Which Node.js code snippet correctly sets an ETag header and handles conditional requests?
Given a resource string, which code snippet correctly sets the ETag header and returns 304 if the client's If-None-Match matches the ETag?
Node.js
const http = require('http');
const resource = 'Hello World';
const etag = '"12345"'; // example ETag

http.createServer((req, res) => {
  // Your code here
}).listen(3000);
A
res.setHeader('ETag', etag);
if (req.headers['if-none-match'] !== etag) {
  res.statusCode = 304;
  res.end();
} else {
  res.statusCode = 200;
  res.end(resource);
}
B
res.setHeader('ETag', etag);
if (req.headers['if-none-match'] === etag) {
  res.statusCode = 304;
  res.end();
} else {
  res.statusCode = 200;
  res.end(resource);
}
C
res.setHeader('ETag', etag);
if (req.headers['if-none-match'] === etag) {
  res.statusCode = 200;
  res.end(resource);
} else {
  res.statusCode = 304;
  res.end();
}
D
res.setHeader('ETag', etag);
res.statusCode = 200;
res.end(resource);
Attempts:
2 left
💡 Hint
Check the condition for matching ETag and the correct status code.
🔧 Debug
advanced
2:00remaining
Why does this Node.js server always send the full resource even if the ETag matches?
Consider this code snippet: const http = require('http'); const resource = 'Hello World'; const etag = '"12345"'; http.createServer((req, res) => { res.setHeader('ETag', etag); if (req.headers['if-none-match'] == etag) { res.statusCode = 304; } res.statusCode = 200; res.end(resource); }).listen(3000); Why does the server always send the full resource body even when the ETag matches?
ABecause the statusCode is overwritten to 200 after setting it to 304, so 304 is never sent
BBecause the ETag header is not set correctly
CBecause the if condition uses == instead of === causing a type mismatch
DBecause the server does not check the If-Modified-Since header
Attempts:
2 left
💡 Hint
Look at the order of setting statusCode and ending the response.
❓ state_output
expert
2:30remaining
What is the response status and body when the client sends a non-matching If-None-Match header?
Given this Node.js server snippet: const http = require('http'); const resource = 'Data v1'; const etag = '"v1"'; http.createServer((req, res) => { res.setHeader('ETag', etag); if (req.headers['if-none-match'] === etag) { res.statusCode = 304; res.end(); } else { res.statusCode = 200; res.end(resource); } }).listen(3000); If the client sends an If-None-Match header with value "v2", what will be the response status and body?
AStatus 304 with no body
BStatus 404 with no body
CStatus 200 with body 'Data v1'
DStatus 500 with error message
Attempts:
2 left
💡 Hint
If the ETag does not match, the server sends the full resource.

Practice

(1/5)
1. What is the main purpose of using an ETag in HTTP responses in Node.js?
easy
A. To set the cache expiration time
B. To encrypt the response data for security
C. To specify the content type of the response
D. To help browsers check if the content has changed and avoid re-downloading unchanged data

Solution

  1. Step 1: Understand what ETag represents

    ETag is a unique identifier for a specific version of a resource, used to detect changes.
  2. Step 2: Identify its role in HTTP caching

    Browsers use ETag to check if the resource changed since last download, avoiding unnecessary data transfer.
  3. Final Answer:

    To help browsers check if the content has changed and avoid re-downloading unchanged data -> Option D
  4. Quick Check:

    ETag purpose = avoid re-download [OK]
Hint: ETag means "same content? skip download" [OK]
Common Mistakes:
  • Confusing ETag with encryption
  • Thinking ETag sets content type
  • Mixing ETag with cache expiration headers
2. Which of the following is the correct way to set an ETag header in a Node.js HTTP response?
easy
A. res.setHeader('ETag', '12345')
B. res.header('etag', 12345)
C. res.etag = '12345'
D. res.set('ETag', 12345)

Solution

  1. Step 1: Recall Node.js HTTP response header syntax

    Use res.setHeader(name, value) to set headers correctly.
  2. Step 2: Check the correct header name and value type

    Header name is case-insensitive but usually 'ETag', and value must be a string.
  3. Final Answer:

    res.setHeader('ETag', '12345') -> Option A
  4. Quick Check:

    Set header with res.setHeader('ETag', value) [OK]
Hint: Use res.setHeader('ETag', 'value') to set ETag [OK]
Common Mistakes:
  • Using incorrect method names like res.header or res.set
  • Passing number instead of string as header value
  • Trying to assign ETag directly as a property
3. Consider this Node.js code snippet handling a request with an ETag check:
const etag = 'abc123';
if (req.headers['if-none-match'] === etag) {
  res.statusCode = 304;
  res.end();
} else {
  res.setHeader('ETag', etag);
  res.end('Hello World');
}

What will the server respond if the client sends header If-None-Match: abc123?
medium
A. Respond with status 200 and body 'Hello World'
B. Respond with status 500 Internal Server Error
C. Respond with status 304 and no body
D. Respond with status 404 Not Found

Solution

  1. Step 1: Check the condition comparing If-None-Match header

    The code compares req.headers['if-none-match'] to etag which is 'abc123'. If equal, it sets status 304.
  2. Step 2: Understand HTTP 304 meaning

    Status 304 means Not Modified, so no body is sent and client uses cached content.
  3. Final Answer:

    Respond with status 304 and no body -> Option C
  4. Quick Check:

    If-None-Match matches ETag = 304 no body [OK]
Hint: If-None-Match equals ETag means 304 Not Modified [OK]
Common Mistakes:
  • Thinking 304 responses include body content
  • Confusing 304 with 200 OK
  • Ignoring the If-None-Match header check
4. This Node.js code tries to handle ETag conditional requests but has a bug:
const etag = 'xyz789';
if (req.headers['If-None-Match'] === etag) {
  res.statusCode = 304;
  res.end();
} else {
  res.setHeader('etag', etag);
  res.end('Content');
}

What is the bug causing the ETag check to fail?
medium
A. ETag value should be a number, not a string
B. Header name case mismatch: 'If-None-Match' vs 'if-none-match'
C. res.end() is missing a callback function
D. res.statusCode must be set after res.end()

Solution

  1. Step 1: Check header name case sensitivity in Node.js

    Node.js lowercases all incoming header names, so req.headers['If-None-Match'] is undefined; correct is req.headers['if-none-match'].
  2. Step 2: Confirm impact on ETag comparison

    Because the header is undefined, the condition never matches, so 304 is never sent.
  3. Final Answer:

    Header name case mismatch: 'If-None-Match' vs 'if-none-match' -> Option B
  4. Quick Check:

    Headers lowercase in Node.js = use 'if-none-match' [OK]
Hint: Node.js headers are lowercase keys [OK]
Common Mistakes:
  • Using wrong case for header keys
  • Thinking ETag must be numeric
  • Misordering res.statusCode and res.end()
5. You want to optimize your Node.js server to use ETags and respond with 304 when content is unchanged. Which approach correctly combines generating an ETag and handling conditional requests?
hard
A. Generate a hash of the response body, set it as ETag header, then compare with req.headers['if-none-match'] to decide 304 or 200
B. Always send status 200 with ETag header, ignoring If-None-Match header
C. Set ETag header only on POST requests and ignore GET requests
D. Use res.setHeader('Cache-Control', 'no-store') to disable caching instead of ETags

Solution

  1. Step 1: Understand ETag generation

    ETags should uniquely represent the current content, often by hashing the response body.
  2. Step 2: Use conditional request header to decide response

    Compare req.headers['if-none-match'] with generated ETag; if equal, respond 304, else send content with new ETag.
  3. Final Answer:

    Generate a hash of the response body, set it as ETag header, then compare with req.headers['if-none-match'] to decide 304 or 200 -> Option A
  4. Quick Check:

    Hash body -> set ETag -> compare If-None-Match -> 304 or 200 [OK]
Hint: Hash content, set ETag, compare If-None-Match for 304 [OK]
Common Mistakes:
  • Ignoring If-None-Match header
  • Sending 200 always without checking ETag
  • Misusing Cache-Control to replace ETag logic
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