casper123
casper123

Reputation: 1766

Get Data from CSV File in nodejs

I have a csv file having about 10k records. I need to retrieve it one by one in my nodejs app.

The scenario is there is when user clicks button "X" first time, the async request is sent to nodejs app which gets data from first row from CSV file. When he clicks again, it'll show data from second row and it keeps on going.

I tried using fast-csv and lazy but all of them read the complete file. Is their a way I can achieve tihs?

Upvotes: 2

Views: 4260

Answers (2)

Fionn Kelleher
Fionn Kelleher

Reputation: 88

Node comes with a readline module in it's core, allowing you to process a readable stream line by line.

var fs = require("fs"),
    readline = require("readline");

var file = "something.csv";

var rl = readline.createInterface({
    input: fs.createReadStream(file),
    output: null,
    terminal: false
})

rl.on("line", function(line) {
    console.log("Got line: " + line);
});

rl.on("close", function() {
    console.log("All data processed.");
});

Upvotes: 4

user548
user548

Reputation: 31

I think the module 'split' by dominic tarr will suffice. It breaks up the stream line by line. https://npmjs.org/package/split

fs.createReadStream(file)
    .pipe(split())
    .on('data', function (line) {
      //each chunk now is a seperate line!
    })

Upvotes: 0

Related Questions