How do you stream a csv file into a node web app?
https://www.npmjs.com/package/busboyhttps://www.npmjs.com/package/fast-csv
var Busboy = require('busboy'); // to handle the formvar csv = require('fast-csv');function (req, res) { var busboy = new Busboy({ headers: req.headers }); busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { file.pipe(csv()) .on('data', function (data) { console.log('YAY, just the data I wanted!', data); }); }); busboy.on('finish', function() { console.log('Done parsing form!'); res.end(); }); req.pipe(busboy);}
I know that the answer was already accepted, but I had a LOT of pain trying to make the answer's code work properly.In the end, what worked out for me was this, still using fast-csv
and busboy
:
var Busboy = require('busboy');var parser = csv();function(req, res) { var busboy = new Busboy({ headers: req.headers }); var result = []; busboy.on('file', function(fieldname, file, filename, encoding, mimetype) { file.on("readable", function () { var data; while ((data = file.read()) !== null) { parser.write(data); } }) .on("end", function () { parser.end(); }); }); parser.on("readable", function () { var data; while ((data = parser.read()) !== null) { //console.log(data); result.push(data); } }) .on("end", function () { console.log("done:",result); res.json(result); }); req.pipe(busboy);}