我试图使用node child_进程执行curl,从本地网络的共享文件夹中获取一个JSON文件(大约220Ko).但它实际上返回了一个我无法解决的缓冲区问题.

var exec = require('child_process').exec;

var execute = function(command, callback){
    exec(command, function(error, stdout, stderr){ callback(error, stdout); });
};

execute("curl http://" + ip + "/file.json", function(err, json, outerr) {
    if(err) throw err;
    console.log(json);
})

下面是我得到的错误:

if(err) throw err;
          ^
Error: stdout maxBuffer exceeded.
    at Socket.<anonymous> (child_process.js:678:13)
    at Socket.EventEmitter.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:746:14)
    at Socket.EventEmitter.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:408:10)
    at emitReadable (_stream_readable.js:404:5)
    at readableAddChunk (_stream_readable.js:165:9)
    at Socket.Readable.push (_stream_readable.js:127:10)
    at Pipe.onread (net.js:526:21)

推荐答案

使用child_process.exec时,需要使用并设置maxBuffer选项.从documentation人中:

maxBuffer指定stdout或stderr上允许的最 Big Data 量-如果超过此值,则子进程将被终止.

文档还指出,maxBuffer的默认值是200KB.

例如,在以下代码中,最大缓冲区大小增加到500KB:

var execute = function(command, callback){
    exec(command, {maxBuffer: 1024 * 500}, function(error, stdout, stderr){ callback(error, stdout); });
};

此外,你可能想阅读大约http.get篇文章,看看它是否能够实现你的目标.

Node.js相关问答推荐

JEST模拟由http服务器控制器导入的ES模块

类扩展值[object object]不是构造函数或null

为什么即使数据库确实更新了,此 POST 也无法解析并返回 200 代码?

在 TypeScript 中正确键入 MongoDB find 方法

发布请求不使用 Nodejs 更新 MongoDB 中的标头信息

为什么运行 yarn 命令会出错 - yargs-parser的完整性判断失败

Axios 响应循环通过函数只返回第一个映射对象的结果

将环境变量从 GitHub 操作传递到 json

使用Typescript 时我应该避免循环导入吗?

FirebaseCloudMessaging : PlatformException (PlatformException(null-error, Host platform returned null value for non-null return value., null, null))

在数组的另一个对象中获取数组的mongoose 对象

如何从 github 编译第三方扩展?

在 Passport 策略回调中获取请求对象

如何将`yarn.lock`与`package.json`同步?

如何监控 node.js 上的网络,类似于 chrome/firefox 开发者工具?

安装 node.JS 时,node.js 运行时和 npm 包管理器选项有什么区别?

Node.js -Firebase 服务帐户私钥不会解析

Fluxible 中的脱水和再水合代表什么?

Meteor - collection.find() 总是返回所有字段

在 Node.js 中获取终端的宽度