0
0
Node.jsframework~10 mins

ETag and conditional requests in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set an ETag header in the response.

Node.js
res.setHeader('ETag', [1]);
Drag options to blanks, or click blank then click option'
AcontentLength
BstatusCode
CetagValue
DrequestUrl
Attempts:
3 left
💡 Hint
Common Mistakes
Using content length or status code instead of ETag value.
2fill in blank
medium

Complete the code to check if the client's ETag matches the server's ETag.

Node.js
if (req.headers['if-none-match'] === [1]) {
Drag options to blanks, or click blank then click option'
AetagValue
BhostHeader
CrequestMethod
DcontentType
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with unrelated headers like content type or host.
3fill in blank
hard

Fix the error in the code to send a 304 Not Modified response when ETags match.

Node.js
if (req.headers['if-none-match'] === [1]) {
  res.statusCode = 304;
  res.end();
}
Drag options to blanks, or click blank then click option'
Areq.headers['etag']
BetagValue
Cres.headers['etag']
Dreq.headers['if-match']
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong header names or response headers for comparison.
4fill in blank
hard

Fill both blanks to generate an ETag from the response body and set it in the header.

Node.js
const crypto = require('crypto');
const etag = crypto.createHash('[1]').update(body).digest('[2]');
res.setHeader('ETag', etag);
Drag options to blanks, or click blank then click option'
Asha256
Butf8
Chex
Dmd5
Attempts:
3 left
💡 Hint
Common Mistakes
Using text encodings like 'utf8' instead of digest encoding.
5fill in blank
hard

Fill all three blanks to handle conditional GET requests with ETag validation.

Node.js
const etag = generateETag(body);
res.setHeader('ETag', [1]);
if (req.headers['if-none-match'] === [2]) {
  res.statusCode = [3];
  res.end();
}
Drag options to blanks, or click blank then click option'
Aetag
B304
CetagValue
D200
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables for ETag header and comparison.
Responding with 200 instead of 304.