2014-12-19 49 views
2

我正在努力研究如何获取衍生子进程的输出并将该输出提供给多部分mime上传。将spawn过程的管道输出转换为superagent upload

这里是我有一个据我可以告诉应该工作

var request = require('superagent'); 
var spawn = require('child_process').spawn; 

var spawned = spawn('echo', ['hello', 'world']); 

request.post('http://localhost/api/upload') 
    .attach('file', spawned.stdout) 
    .end(function(res) { 
     console.log("DONE", res); 
    }); 

不幸的是,这将引发从节点而无益Error: socket hang up响应。

回答

0

你很贴心!

下面是最终版本,你想要做什么:

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

var request = require('superagent'); 

exec('echo hello world', function(err, stdout, stderr) { 
    request.post('http://localhost/api/upload') 
    .attach('file', stdout) 
    .end(function(res) { 
     console.log('DONE', res); 
}); 

我使用exec在这里,因为它的回调函数输出的输出和错误流,它似乎像你想要的。

+0

感谢您的建议。 我重写了你的例子,因为Superagent真的不喜欢用缓冲区而不是Streams。我用下面的代码替换了它:https://gist.github.com/hash-bang/f897f72fa8048ebb315d 这个的确如你所说的那样工作。可惜我找不到用Streams做这项工作的方法。 – 2014-12-21 06:40:56

+0

啊,没有意识到:对不起,< – rdegges 2014-12-21 20:50:16

+0

你的方法工作正常,只是使用缓冲区而不是Streams。经过多次实验,我仍然无法找到一种方法来完成这项工作。 – 2014-12-21 20:52:43