3

I want to allow users to use a form to upload (POST) an image file to my Express.js web application. Upon receipt, my web application will POST it to the GetChute API (an image web service).

I don't want my server temporarily writing the image to disk - instead it should store the image to a variable, and send it to GetChute.

By default, 2 Express middleware items (Connect and Formidable) I think are providing a nice way of automatically doing disk writes from POSTs, but how would I circumvent them to directly access the bytestream so I can avoid a disk write?

I suspect that someone smarter than me can come up with a tidier solution than was arrived at for a similar question recently:

Stream file uploaded with Express.js through gm to eliminate double write

Thanks!

Community
  • 1
  • 1
Adam
  • 464
  • 5
  • 16
  • Why can't you just read those values into a var in node and push them back out? Is it because you're relying on an express plugin to capture the file uploads? – jcolebrand Aug 06 '12 at 17:50
  • 1
    It's because Express's default form handling middleware (Connect & Formidable) abstract the stream so it can't be directly accessed as far as I can tell. Let me know if you find a way to do it without modifying Express's guts. :) – Adam Aug 06 '12 at 23:17

1 Answers1

0

Can you directly stream the request to GetChute ?

app.post('/upload', function(req, res) {
  var chuteReq = http.request({
    host: 'api.getchute.com',
    path: '/x/y/z',
    method: 'POST'
  }, function(chuteRes) {
    var chuteBody = '';

    chuteRes.on('data', function(chunk) {
      chuteBody += chunk;
    });

    chuteRes.on('end', function() {
      var chuteJSON = JSON.parse(chuteBody);

      // check the result and build an appropriate response.
      res.end(chuteJSON);
    });
  });

  // pipe the image data directly to GetChute
  req.pipe(chuteReq);      
});

Of course, you can adjust the request to GetChute. Be careful, you might have to decode the multipart/form-data.

Community
  • 1
  • 1
Laurent Perrin
  • 14,671
  • 5
  • 50
  • 49