将产卵过程的标准输出转化为超级上传

我正在努力解决如何获取产生的subprocess的输出,并将该输出提供给多部分MIME上载。

这是我所知道的,据我所知应该工作

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来自Node的响应。

你非常接近!

这是最终的版本,它是你想要的:

 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 ,因为它是callback函数输出stdout和stderrstream,它看起来像你想要的。