Node.js › HTTP & Web Servers
How would you implement HTTP response streaming in Node.js to begin sending data to a client before the entire response is ready, such as for a large report?
AdvancedAnswer
Rather than building a complete response in memory before calling res.end() once, you can call res.write() multiple times as data becomes progressively available, letting the client start receiving and processing data before the entire operation on the server side has finished -- particularly valuable for large exports or reports where waiting for the entire dataset to be assembled before sending anything would introduce significant unnecessary latency for the client.
app.get('/export', async (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/csv', 'Transfer-Encoding': 'chunked' });
res.write('id,name,email\n');
for await (const batch of getRecordsInBatches()) {
res.write(batch.map(r => `${r.id},${r.name},${r.email}\n`).join(''));
}
res.end();
});Real-world example A CSV export endpoint streams rows to the client as they're fetched from the database in batches, letting a download begin immediately and progressively fill in, rather than the user waiting with no feedback until the entire multi-hundred-thousand-row export had been fully assembled server-side first.