0
0
Node.jsframework~3 mins

Why ETag and conditional requests in Node.js? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how a tiny code trick can make your website lightning fast and save tons of data!

The Scenario

Imagine you have a website that serves images and data to thousands of users. Every time someone visits, your server sends the full content again, even if nothing changed since their last visit.

The Problem

Sending full content every time wastes bandwidth, slows down loading, and overloads your server. Users wait longer, and your server works harder for no good reason.

The Solution

ETags let your server tell the browser if content has changed. The browser asks, "Is this content still the same?" If yes, the server replies "Not modified" and sends nothing new. This saves time and data.

Before vs After
Before
res.sendFile('image.png'); // sends full file every time
After
res.set('ETag', '12345'); if (req.headers['if-none-match'] === '12345') { res.status(304).end(); } else { res.sendFile('image.png'); }
What It Enables

This makes websites faster and lighter by only sending data when it really changes.

Real Life Example

When you refresh a news website, it only downloads new articles, not the whole page again, thanks to ETags and conditional requests.

Key Takeaways

Manual full data sending wastes resources and slows users down.

ETags let servers check if content changed before sending.

Conditional requests speed up websites and save bandwidth.