2

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?

2
  • There's a plugin for that Commented Dec 15, 2013 at 16:04
  • @adeneo .. isn't there any way to do it without any external plugins? Commented Dec 16, 2013 at 6:00

2 Answers 2

4

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.");
});
Sign up to request clarification or add additional context in comments.

2 Comments

rl.on("line") reads the complete file line-by-line .. I need to read each line once we get request from client
So you want to read the file line by line when you get a HTTP request or something? I think I'm missing what you're trying to say. Care to update your question with an example?
0

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!
    })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.