nodejs 请求 - toString 失败 - 大文件的缓冲区大小问题 (maxBufferSize)

nodejs request - toString failed - buffer size issue for big files (maxBufferSize)

本文关键字:问题 maxBufferSize 缓冲区 文件 请求 toString 失败 nodejs      更新时间:2023-09-26

我请求一个包含 150K 条记录的大文件,但它抛出"toString 失败"错误。 nodejs/node#3175 说这是因为 maxBufferSize。请求适用于 200 条记录,但它是外部 API,要求是一次获取所有记录。[没有分页:(]有没有办法为此请求设置缓冲区大小?

我已经在这里问过这个问题

编辑:

request("http://www.site-containing-big-data/api",
        function (error, response, body) {
            console.log('got something to show');
            if(!error && response.statusCode == 200) {
                resolve(body);
            }else if(error){
                reject(error);
            }
        });

但控制台中除了toString failed消息外没有任何显示

解决了它。这是一个xml文件,我试图直接处理它。现在,我首先保存在文件中,然后使用xml-stream逐个处理每个对象。

request.get("http://www.site-containing-big-data/api" )
        .on('error', function(errReq) {
            console.log('error while reading from big site : ', errReq);
        }).on('end', function(){
            console.log('got from big site, now processing');
            var XmlStream = require('xml-stream') ;
            var stream=fs.createReadStream('bigfile.xml');
            xml = new XmlStream(stream);
            xml.on('endElement: item', function(item) {//item is a node. structure is <items><item></item><item></item></items>
                //do something here
            });
            xml.on('end', function(){
                // when processing finished for all objects/items in that file
                });
            });
        })
        .pipe(fs.createWriteStream('bigfile.xml'));