Home > Web Front-end > JS Tutorial > Solutions to problems encountered when reading large text files in nodejs

Solutions to problems encountered when reading large text files in nodejs

不言
Release: 2018-11-08 15:11:46
Original
3634 people have browsed it

This article introduces to you the solutions to problems encountered when reading large text files in nodejs. Friends in need can refer to it.

Been playing around with NodeJS lately and encountered the following error when trying to read a very large text file:

FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of memory
Copy after login

The following solution allows you to stream the file instead of reading it in its entirety Into memory:

var fs = require('fs');
var readline = require('readline');
var stream = require('stream');
var instream = fs.createReadStream('your/file');
var outstream = new stream;
var rl = readline.createInterface(instream, outstream);
rl.on('line', function(line) {
  // process line here
});
rl.on('close', function() {
  // do something on finish here
});
Copy after login


The above is the detailed content of Solutions to problems encountered when reading large text files in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template