How to read large binary files in node js without a blocking loop?

Muhammad Ali picture Muhammad Ali · Mar 21, 2016 · Viewed 8.5k times · Source

I am trying to learn some basics of event driven programming. So for an exercise I am trying to write a program that reads a large binary file and does something with it but without ever making a blocking call. I have come up with the following:

var fs = require('fs');
var BUFFER_SIZE = 1024;
var path_of_file = "somefile"

fs.open(path_of_file, 'r', (error_opening_file, fd) =>
{
    if (error_opening_file)
    {
        console.log(error_opening_file.message);
        return;
    }

    var buffer = new Buffer(BUFFER_SIZE);
    fs.read(fd, buffer, 0, BUFFER_SIZE, 0, (error_reading_file, bytesRead, buffer) =>
    {
        if (error_reading_file)
        {
            console.log(error_reading_file.message);
            return;
        }

        // do something e.g. print or write to another file
    })
})

I know I need to put a while loop in order to read complete file but in the above code I am reading just the first 1024 bytes of the file and cannot formulate how to continue reading the file without using a blocking loop. How could we do it?

Answer

rgvassar picture rgvassar · Mar 21, 2016

Use fs.createReadStream instead. This will call your callback over and over again until it has finished reading the file, so you don't have to block.

var fs = require('fs');

var readStream = fs.createReadStream('./test.exe');
readStream.on('data', function (chunk) {
  console.log(chunk.length);
})