SyntaxStudy
Sign Up
Node.js Piping and File Streaming
Node.js Beginner 10 min read

Piping and File Streaming

pipe() connects a readable stream to a writable stream automatically managing backpressure — the mechanism that prevents a fast producer from overwhelming a slow consumer. The modern pipeline() utility handles errors and cleanup better than manual pipe().

Example
const fs       = require('fs');
const zlib     = require('zlib');
const { pipeline } = require('stream/promises'); // Node 15+
const http     = require('http');

// Compress a file using pipeline (handles errors + cleanup):
async function gzipFile(input, output) {
    await pipeline(
        fs.createReadStream(input),
        zlib.createGzip(),
        fs.createWriteStream(output)
    );
    console.log(`Compressed ${input} -> ${output}`);
}

gzipFile('large-file.log', 'large-file.log.gz').catch(console.error);

// Stream a file as an HTTP response (memory-efficient):
const server = http.createServer((req, res) => {
    if (req.url === '/download') {
        res.setHeader('Content-Type', 'application/octet-stream');
        res.setHeader('Content-Disposition', 'attachment; filename="data.csv"');

        const fileStream = fs.createReadStream('data.csv');
        fileStream.on('error', () => res.status(500).end('File not found'));
        fileStream.pipe(res);
    } else {
        res.end('OK');
    }
});

server.listen(3000, () => console.log('Streaming server on 3000'));