I'm very new to node / Express. In my node / express 4 code I'm first converting a video file into a GIF and then uploading to Amazon S3. The GIF operation is long running and the upload can take some time. After this has been done I then return a URL back to the client pointing to the file. I create a short URL in my node code which is also used for the GIF filename. What I'm proposing to do is immediately return the URL (which is still rendering) to the client and continue the GIF operation and upload in the background to create a better user experience. By the time the user goes to the URL it will likely be rendered and available to download. My problem is that the render and upload code doesn't continue after I have sent the response JSON containing the URL. My understanding is that the HTTP connection will close after a response is sent, but the operation can continue? If I nest the res in the S3 upload completion block, it works fine.
app.post("/post", function(req, res) {
var fileRef;
var response = res;
var host = req.get('host');
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
// version 1. res immediately and continue creating the gif. (this doesn't work)
var gifURL = "http://foo.com/" + filename.slice(0, -4);
response.json({ url: gifURL});
fileRef = __dirname + filename + "." + shorfilename;
console.log("Uploading to: " + fileRef);
fstream = fs.createWriteStream(fileRef);
file.pipe(fstream);
fstream.on('close', function () {
var opts = {
height: 320,
rate: 10
};
var tmpGif = shorfilename + ".gif";
console.time('convert');
gify(fileRef, tmpGif, opts, function(err){
if (err) throw err;
console.timeEnd('convert');
var s = fs.statSync(tmpGif);
client.putFile(tmpGif, shorfilename + ".gif", function(err, res){
fs.unlink(tmpGif, function (err) {
if (err) response.errors.push("Error : " + err);
});
console.log("Created: " + res.req.url);
var url = res.req.url;
var idx = url.lastIndexOf('/');
var filename = url.substring(idx + 1);
// version 2. res after render and upload. this works
var gifURL = "http://foo.com/" + filename.slice(0, -4);
response.json({ url: gifURL});
});
});
});
});
});