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?
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); } });
Think about what the 304 status code means in HTTP.
If the client's If-None-Match header matches the server's ETag, the server responds with 304 Not Modified and no body, telling the client to use its cached version.
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
});app.get('/file', (req, res) => { const lastModified = new Date('2024-01-01T12:00:00Z'); // Your code here });
Remember to compare dates as Date objects, not strings.
Option C correctly sets the Last-Modified header as a UTC string and compares the client's If-Modified-Since date as a Date object to decide if the resource is modified.
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?
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(); } });
Check the exact value format of the If-None-Match header sent by browsers.
Browsers send ETag values with quotes, e.g., "abc123". The code compares without quotes, so the condition is always true, sending full content.
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?
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'); } });
Compare the dates carefully to see if the resource is newer than the client's cached version.
The client's If-Modified-Since date is before the resource's Last-Modified date, so the resource has changed. The server sends status 200 with the updated content.
To implement efficient conditional GET caching in an Express app, which combination of headers should be used together?
Think about which headers allow precise validation of resource changes.
ETag and If-None-Match headers provide a strong validator for resource changes, enabling precise conditional GET requests. Last-Modified is less precise and can be used alone but ETag is preferred for accuracy.