patternjavascriptModerate
Chunked Transfer Encoding for Dynamic Response Streams
Viewed 0 times
chunked encodingtransfer-encodingcontent-lengthstreamingres.write
Problem
A server generating a large or infinite response cannot set
Content-Length upfront, causing the client to wait for the full response or the connection to close unexpectedly.Solution
Use chunked transfer encoding: HTTP/1.1 allows sending
In Node.js,
Transfer-Encoding: chunked without a Content-Length. Each chunk is prefixed with its size in hex.In Node.js,
res.write() automatically enables chunked encoding when res.end() hasn't been called.app.get('/stream', async (req, res) => {
res.setHeader('Content-Type', 'application/json');
// Do NOT call res.json() or res.send() — use write/end
res.write('[');
let first = true;
for await (const item of generateItems()) {
if (!first) res.write(',');
res.write(JSON.stringify(item));
first = false;
}
res.write(']');
res.end();
});Why
Without chunked encoding, the server must buffer the entire response to calculate Content-Length, defeating streaming. Chunked encoding signals the end of each chunk, and a zero-length chunk signals end of the response.
Gotchas
- HTTP/2 does not use the chunked transfer encoding mechanism — framing is handled at the HTTP/2 layer.
- Some proxies buffer chunked responses before forwarding, eliminating streaming benefits.
- Setting Content-Length overrides chunked encoding — don't set both.
Revisions (0)
No revisions yet.