user2740466
user2740466

Reputation: 47

Node.js & Express: Serving text files in parts

I have a webserver with node.js and express. When I serve a text file that is big, it takes a lot of time to load into the browser. Imagine a 34 MB text file being served to the client, he would have to download the 34 MB only to check for something in the log file.

Is there something I could do about it? Serve it in parts perhaps? Like: Part 1/10 - Part 10/10

Upvotes: 0

Views: 760

Answers (2)

hexacyanide
hexacyanide

Reputation: 91599

To serve a file partial, you could take a byte range, and read that range with the filesystem function fs.createReadStream(). For example, if you wanted one tenth of a file, you could measure its size, and calculate the byte range.

app.get('/log', function(req, res) {
  fs.stat('log.txt', function(err, stats) {
    var end = Math.ceil(stats.size / 10);
    var stream = fs.createReadStream('log.txt', {start: 0, end: end});

    var log = new String();
    readable.on('data', function(chunk) {
      log += chunk;
    });

    readable.on('end', function() {
      res.send(log);
    });
  });
});

You could always modify the code to send from 10-20%, or to send the last 10% of the file, etc.

Upvotes: 1

headwinds
headwinds

Reputation: 1851

Have you looked into Node streams? You can use them to send your data in small chunks, and offer feedback to show how much of it has been downloaded.

Upvotes: 0

Related Questions