0
0
Node.jsframework~20 mins

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

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