Search code examples
node.jsexpressjson2csv

How to stream a file without saving it down first?


I have put together the below code that creates a CSV called example.csv, using the json2csv library.

I would prefer to not have to save down and store the CSV file before it is passed to the front end to be downloaded.

I can't seem to figure out how to stream or pipe the file to the front end, without saving it first.

How to take the output CSV file of the json2csv library and send it straight tot he front end?

Some of my code

    const input = new Readable({ objectMode: true });
    input._read = () => {};
    input.push(JSON.stringify(parsed));
    input.push(null);

    var outputPath = "example.csv";
    const output = createWriteStream(outputPath, { encoding: "utf8" });
    const json2csv = new Transform(opts, transformOpts);

    // Pipe to output path
    input.pipe(json2csv).pipe(output);
    
    res.setHeader("Content-Type", "text/csv");
    res.download(outputPath);

Solution

  • You can simply pipe the json2csv stream to the res object, e.g:

    const ReadableStream = require('stream').Readable;
    const Json2csvTransform = require('json2csv').Transform;
    
    app.get('/csv', (req, res) => {
        const stream = new ReadableStream();
        stream.push(JSON.stringify(data));
        stream.push(null);
    
        const json2csv = new Json2csvTransform({}, transformOpts);
        res.setHeader('Content-disposition', 'attachment; filename=data.csv');
        res.writeHead(200, { 'Content-Type': 'text/csv' });
        stream.pipe(json2csv).pipe(res);
    })