📖 Scenario: You are building a simple Node.js server that serves a text file. To improve efficiency, you want to use ETag headers so the server can tell the browser if the file has changed. If the file is unchanged, the server will respond with a 304 status, telling the browser to use its cached copy.
🎯 Goal: Build a Node.js server that sends an ETag header with the file content. The server should check the If-None-Match header from the request and respond with 304 Not Modified if the ETag matches, or send the file content with a 200 OK status if it does not.
📋 What You'll Learn
Create a variable with the file content string exactly as specified
Create a variable that stores the ETag string for the file content
Write a server request handler that checks the If-None-Match header
Send a 304 status if the ETag matches, otherwise send the file content with the ETag header
💡 Why This Matters
🌍 Real World
ETags help browsers cache files efficiently by letting servers tell if a file has changed. This reduces data usage and speeds up page loads.
💼 Career
Understanding ETags and conditional requests is important for backend developers working on web servers and APIs to optimize performance and bandwidth.
Progress0 / 4 steps
1
DATA SETUP: Create the file content variable
Create a variable called fileContent and set it to the string 'Hello, this is the file content.'
Node.js
Hint
Use const fileContent = 'Hello, this is the file content.'; to create the variable.
2
CONFIGURATION: Create the ETag variable
Create a variable called etag and set it to the string '"12345"' (including the quotes inside the string)
Node.js
Hint
Remember to include the double quotes inside the string for the ETag value.
3
CORE LOGIC: Write the request handler to check If-None-Match
Write a function called requestHandler that takes req and res as parameters. Inside it, get the If-None-Match header from req.headers and store it in a variable called ifNoneMatch. Then use an if statement to check if ifNoneMatch equals the etag variable.
Node.js
Hint
Use req.headers['if-none-match'] to get the header value.
4
COMPLETION: Complete the response logic with 304 or 200 and ETag
Inside the requestHandler function, complete the if block to send a 304 status with res.statusCode = 304 and res.end(). In the else block, set res.statusCode = 200, set the ETag header to the etag variable using res.setHeader, and send the fileContent with res.end(fileContent).
Node.js
Hint
Use res.statusCode, res.setHeader, and res.end() to send the response.
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
Step 1: Understand what ETag represents
ETag is a unique identifier for a specific version of a resource, used to detect changes.
Step 2: Identify its role in HTTP caching
Browsers use ETag to check if the resource changed since last download, avoiding unnecessary data transfer.
Final Answer:
To help browsers check if the content has changed and avoid re-downloading unchanged data -> Option D
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?
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
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'].
Step 2: Confirm impact on ETag comparison
Because the header is undefined, the condition never matches, so 304 is never sent.
Final Answer:
Header name case mismatch: 'If-None-Match' vs 'if-none-match' -> Option B
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
Step 1: Understand ETag generation
ETags should uniquely represent the current content, often by hashing the response body.
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.
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
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 unknownstrongwhile earning +1 Grip toward your certificate.