Complete the code to check if the request has an 'If-None-Match' header.
if (req.headers['[1]']) { res.status(304).end(); }
The 'If-None-Match' header is used to make conditional requests based on ETag values.
Complete the code to set the ETag header in the response.
res.set('[1]', '12345');
The 'ETag' header is used to identify a specific version of a resource.
Fix the error in the code to properly send a 304 Not Modified response.
if (req.headers['if-none-match'] === etag) { res.[1](304).end(); }
send(304) which sends 304 as body, not status.write() without ending the response.Use res.status(304).end() to set the status code and end the response.
Fill both blanks to create a middleware that checks ETag and sends 304 if matched.
app.use((req, res, next) => {
const etag = 'abc123';
if (req.headers['[1]'] === etag) {
res.[2](304).end();
} else {
res.set('ETag', etag);
next();
}
});The middleware checks the 'If-None-Match' header and uses res.status(304).end() to respond.
Fill all three blanks to create a route that handles conditional GET with ETag and Last-Modified.
app.get('/resource', (req, res) => { const etag = 'xyz789'; const lastModified = new Date('2024-01-01'); res.set('[1]', etag); res.set('[2]', lastModified.toUTCString()); if (req.headers['if-none-match'] === etag || (req.headers['if-modified-since'] && new Date(req.headers['if-modified-since']) >= lastModified)) { res.[3](304).end(); } else { res.send('Resource content'); } });
The route sets both 'ETag' and 'Last-Modified' headers and uses res.status(304).end() to respond if the resource is unchanged.