最近一直在玩NodeJS,并在尝试读取非常大的文本文件时遇到以下错误:
FATAL ERROR: CALL_AND_RETRY_0 Allocation failed - process out of 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 });
以上就是在nodejs中读取大文本文件时遇到问题的解决方案的详细内容,更多请关注php中文网其它相关文章!
……