Complete the code to set a Cache-Control header that tells browsers to cache the response for 1 hour.
app.get('/data', (req, res) => { res.set('[1]', 'public, max-age=3600'); res.send({ message: 'Hello' }); });
The Cache-Control header controls how browsers cache responses. Setting it to public, max-age=3600 means the response can be cached for 3600 seconds (1 hour).
Complete the code to generate an ETag header using the express built-in method.
app.get('/resource', (req, res) => { res.[1](); res.send('Resource content'); });
Express has a built-in etag() method to generate and set the ETag header automatically.
Fix the error in the code to properly check the client's ETag and respond with 304 if it matches.
app.get('/file', (req, res) => { const etag = '12345'; if (req.headers['if-none-match'] === [1]) { res.status(304).end(); } else { res.set('ETag', etag); res.send('File content'); } });
The variable etag holds the ETag string. We compare the header value to this variable without quotes.
Fill both blanks to set Cache-Control to no-store and disable ETag generation.
app.use((req, res, next) => {
res.set('[1]', 'no-store');
app.disable('[2]');
next();
});Setting Cache-Control to no-store tells browsers not to cache. Disabling etag stops Express from generating ETags.
Fill both blanks to create a middleware that sets Cache-Control to private, sets ETag, and sends JSON response.
app.get('/profile', (req, res) => { res.set('[1]', 'private, max-age=600'); res.[2](); res.send({ user: 'Alice', id: 1, }); });
The middleware sets Cache-Control to private with max-age 600 seconds, calls etag() to generate the ETag header, and sends a JSON object with a comma separating properties.