QA
Intermediate LevelExpress JS

How do you tune HTTP caching in Express by setting Cache-Control, ETag, and Last-Modified headers?

❓ Interview Prompt: Explain HTTP Cache-Control directives (max-age, s-maxage, stale-while-revalidate), ETag generation, and CDN edge caching.

Configuring correct Cache-Control headers allows browsers and CDN edge nodes to cache responses, reducing database queries and latency.

Key Directives:

•
'max-age=3600': Browser caches response for 1 hour.
•
's-maxage=86400': CDN edge nodes cache response for 24 hours.
•
'stale-while-revalidate=60': Serves stale cached content instantly while fetching fresh data in the background.

Code Implementation Example

javascript
import express from 'express';
const app = express();

app.get('/api/catalog', (req, res) => {
  // Instruct CDN to cache for 24h and browsers for 1 hour
  res.set('Cache-Control', 'public, max-age=3600, s-maxage=86400, stale-while-revalidate=300');
  res.json({ catalog: ['Item A', 'Item B'] });
});
💡Tuning Cache-Control headers for browser and CDN edge caching.
🎯 Interview Key Takeaway

Set explicit Cache-Control headers (max-age, s-maxage, stale-while-revalidate) to optimize browser and CDN caching.

#Cache-Control#Caching#Headers#Intermediate
Back to all Express JS Questions→

More Express JS Interview Questions

Beginner LevelExpress JS

What is Express.js and how does the middleware pipeline work?

Beginner LevelExpress JS

What is the difference between req.params, req.query, and req.body?

Beginner LevelExpress JS

How does express.Router() work for building modular route controllers?