2

我正在努力研究如何获取生成的子进程的输出并将该输出输入到多部分 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.js 的相当无益的响应。

4

1 回答 1

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这里使用,因为它的回调函数输出 stdout 和 stderr 流,这似乎是你想要的。

于 2014-12-19T16:58:03.370 回答