0
0
Expressframework~20 mins

Conditional requests handling in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Requests Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when a client sends an If-None-Match header matching the ETag?

Consider this Express route that serves a resource with an ETag header:

app.get('/resource', (req, res) => {
  const data = 'Hello World';
  const etag = '12345';
  res.set('ETag', etag);
  if (req.headers['if-none-match'] === etag) {
    res.status(304).end();
  } else {
    res.send(data);
  }
});

If a client sends a request with header If-None-Match: 12345, what will the server respond with?

Express
app.get('/resource', (req, res) => {
  const data = 'Hello World';
  const etag = '12345';
  res.set('ETag', etag);
  if (req.headers['if-none-match'] === etag) {
    res.status(304).end();
  } else {
    res.send(data);
  }
});
AStatus 500 Internal Server Error
BStatus 304 with no body
CStatus 404 Not Found
DStatus 200 with body 'Hello World'
Attempts:
2 left
💡 Hint

Think about what the 304 status code means in HTTP.

📝 Syntax
intermediate
2:00remaining
Which option correctly sets Last-Modified header and handles If-Modified-Since?

Given this Express route, which option correctly implements conditional GET using Last-Modified?

app.get('/file', (req, res) => {
  const lastModified = new Date('2024-01-01T12:00:00Z');
  // Your code here
});
Express
app.get('/file', (req, res) => {
  const lastModified = new Date('2024-01-01T12:00:00Z');
  // Your code here
});
A
res.set('Last-Modified', lastModified.toUTCString());
if (req.headers['if-modified-since'] > lastModified.toUTCString()) {
  res.status(304).end();
} else {
  res.send('File content');
}
B
res.set('Last-Modified', lastModified);
if (req.headers['if-modified-since'] === lastModified.toUTCString()) {
  res.status(304).end();
} else {
  res.send('File content');
}
C
res.set('Last-Modified', lastModified.toUTCString());
if (new Date(req.headers['if-modified-since']) >= lastModified) {
  res.status(304).end();
} else {
  res.send('File content');
}
D
res.set('Last-Modified', lastModified.toISOString());
if (new Date(req.headers['if-modified-since']) < lastModified) {
  res.status(304).end();
} else {
  res.send('File content');
}
Attempts:
2 left
💡 Hint

Remember to compare dates as Date objects, not strings.

🔧 Debug
advanced
2:00remaining
Why does this Express code always send the full response ignoring If-None-Match?

Examine this code snippet:

app.get('/data', (req, res) => {
  const etag = 'abc123';
  res.set('ETag', etag);
  if (req.headers['if-none-match'] !== etag) {
    res.send('Data content');
  } else {
    res.status(304).end();
  }
});

Clients report they always get the full content, even when sending If-None-Match: abc123. What is the bug?

Express
app.get('/data', (req, res) => {
  const etag = 'abc123';
  res.set('ETag', etag);
  if (req.headers['if-none-match'] !== etag) {
    res.send('Data content');
  } else {
    res.status(304).end();
  }
});
AThe If-None-Match header may contain quotes, so direct string comparison fails
BETag header must be set after sending the response
CThe comparison should use strict equality === instead of !==
DThe status 304 must be sent before setting the ETag header
Attempts:
2 left
💡 Hint

Check the exact value format of the If-None-Match header sent by browsers.

state_output
advanced
2:00remaining
What is the response status and body when If-Modified-Since is before Last-Modified?

Given this Express route:

app.get('/info', (req, res) => {
  const lastModified = new Date('2024-06-01T10:00:00Z');
  res.set('Last-Modified', lastModified.toUTCString());
  const ifModifiedSince = new Date(req.headers['if-modified-since']);
  if (ifModifiedSince >= lastModified) {
    res.status(304).end();
  } else {
    res.send('Updated info');
  }
});

If the client sends If-Modified-Since: 2024-05-01T10:00:00Z, what will the server respond?

Express
app.get('/info', (req, res) => {
  const lastModified = new Date('2024-06-01T10:00:00Z');
  res.set('Last-Modified', lastModified.toUTCString());
  const ifModifiedSince = new Date(req.headers['if-modified-since']);
  if (ifModifiedSince >= lastModified) {
    res.status(304).end();
  } else {
    res.send('Updated info');
  }
});
AStatus 200 with body 'Updated info'
BStatus 304 with no body
CStatus 400 Bad Request
DStatus 500 Internal Server Error
Attempts:
2 left
💡 Hint

Compare the dates carefully to see if the resource is newer than the client's cached version.

🧠 Conceptual
expert
2:00remaining
Which HTTP header combination best supports efficient conditional GET caching?

To implement efficient conditional GET caching in an Express app, which combination of headers should be used together?

AETag and Cache-Control headers
BLast-Modified and If-Modified-Since headers
CCache-Control and Pragma headers
DETag and If-None-Match headers
Attempts:
2 left
💡 Hint

Think about which headers allow precise validation of resource changes.