0

In Node Express, I am reading a locally saved zip file and sending it to a Java servlet using request module.

var req = require('request');
//Define a read stream from our source zip file
 var source = fs.createReadStream('abc.zip');
//Send our data via POST request
source.pipe(req.post('http://myhost:8080/myservlets/uploadzip')); 
//Can I cleanup abc.zip here?

After the zip is uploaded, I need to clean up the zip from the node server. The challenge that I am facing is that how to find that zip upload has completed? Is there a way to get a promise when upload completes or define a callback, so that I can put the zip cleanup code only after upload completes.

swati
  • 1,157
  • 2
  • 16
  • 25
  • Possible duplicate of [createReadStream().pipe() Callback](http://stackoverflow.com/questions/14962085/createreadstream-pipe-callback) – Michael Troger Sep 14 '16 at 16:09

1 Answers1

0

I think this is the same as asked here: createReadStream().pipe() Callback For your case it should work like:

source.on('close', function(){
  fs.unlink('abc.zip');
});

Meaning you just have to overwrite the callback included in fs. In loganfsmyth's answer you can find a way to extend this for keeping files, which had errors on submitting by overwriting the error callback.

Michael Troger
  • 3,336
  • 2
  • 25
  • 41