简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM