In this lesson, you will learn how HTTP servers tell the client how much data to expect when sending a response body.
When a server sends data to a client, the receiver needs to know when the message ends. Since TCP is a stream-based protocol, the receiver doesn't naturally know where one "message" stops and the next begins. HTTP provides two primary ways to handle this.
The Content-Length Header
The Content-Length approach is for scenarios where you know exactly how many bytes you are sending before you start. You calculate the size of your payload, set the Content-Length header, and then send the data.
// Example: Sending a fixed-length response
const body = "Hello World!";
response.writeHead(200, {
'Content-Length': Buffer.byteLength(body),
'Content-Type': 'text/plain'
});
response.end(body);
This is efficient because the client can pre-allocate memory and show a progress bar. However, it requires you to hold the entire body in memory (or have it fully calculated) before you can start the response.
Chunked Transfer Encoding
What if you are streaming data, such as a large file or a live log, where the total length isn't known upfront? This is where Transfer-Encoding: chunked shines. Instead of one big block, you send the data in a series of smaller pieces (chunks).
Each chunk is preceded by its size in hexadecimal, followed by the actual data. The transfer concludes with a special "zero-length" chunk that signals the end of the stream.
// Example: Concept of chunked response
response.writeHead(200, {
'Transfer-Encoding': 'chunked'
});
response.write("Part one of the data...");
response.write("Part two of the data...");
response.end(); // The stream automatically signals the end
This is ideal for high-performance Node.js applications using streams because it allows you to pipe data from a source (like a database or file system) directly to the client without buffering the entire payload in your server's RAM.
Key Takeaway: Use Content-Length when the total size is known to optimize for memory, and use Transfer-Encoding: chunked when streaming dynamic data to avoid buffering large payloads.