2

I'm struggling to work out how to take the output of a spawned child process and feed that output into a multipart mime upload.

Here is what I have that as far as I can tell should work

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);
    });

Unfortunately this throws the rather unhelpful Error: socket hang up response from Node.

hash-bang
  • 492
  • 1
  • 4
  • 9

1 Answers1

0

You're quite close!

Here's the final version which does what you want:

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);
});

I'm using exec here, because it's callback function outputs the stdout and stderr streams, which it seems like you want.

rdegges
  • 32,786
  • 20
  • 85
  • 109
  • Thanks for your suggestion. I re-wrote your example a little as Superagent really don't like using buffers instead of Streams. I've replaced it in the below with Request instead: https://gist.github.com/hash-bang/f897f72fa8048ebb315d This does work as you said. Shame I can't find a means of making this work with Streams instead though. – hash-bang Dec 21 '14 at 06:40
  • Ah, didn't realize that :( Sorry > – rdegges Dec 21 '14 at 20:50
  • You're method works fine though. Just uses buffers instead of Streams. After much experimentation I still can't find a way of making this work. – hash-bang Dec 21 '14 at 20:52