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!
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.