我正在努力弄清楚如何获取生成的子进程的输出并将该输出输入到多部分 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);
});
不幸的是,这会引发来自 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
,因为它的回调函数输出 stdout 和 stderr 流,这似乎是您想要的。
关于node.js - 将spawn进程的STDOUT通过管道传输到superagent上传中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27559320/