在node.js中从pipe道传输大量数据时截断

我试图通过使用节点的subprocess通过HTTPstream任意大量的数据。 我的完整代码在这里 ,显着位是:

res.writeHead(200, { 'Content-Type': 'application/octet-stream', 'Content-Disposition': 'attachment;filename=osm_export_' + ((north + south) / 2) + '_' + ((east + west) / 2) + '.pbf' }); // run vex var proc = spawn(cmd, [dbname, south, west, north, east, '-']); // stream chunks proc.stdout.pipe(res); 

大约40 MB(40,000,000和42,000,000字节之间的任何地方)之后,数据stream将被中断,请求也不会完成。 我不能设置一个Content-Length头,因为我不知道多less数据的命令将产生,直到完成我想知道这是否是一个缓冲区欠载, 有问题的命令是从数据库中提取数据并写入一个stream,这可能比我的电脑和我的服务器之间的连接速度慢。 我怀疑这是因为我用这个replace了代码:

 var http = require('http'); var spawn = require('child_process').spawn; http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'application/octet-stream'}); var proc = spawn('head', ['-c', '500000000', '/dev/zero']); proc.stdout.pipe(res); }).listen(8181, '127.0.0.1'); 

stream500MB的空数据,它工作正常。 是否有某种超时等,我需要设置?

啊,发现了这个问题。 这可能会或可能不会对其他人有用。 我没有对stream程stderrstream做任何事情,后端进程写了很多状态信息到stderr。 某个节点内的缓冲区被填满,然后崩溃。 将stream程创build行更改为

  var proc = spawn(cmd, [dbname, south, west, north, east, '-'], {stdio: ['ignore', 'pipe', 'ignore']}); 

解决了这个问题。 感谢所有帮助!

尝试设置HTTP头Content-Length并将其值设置为您在响应中使用的数据大小。