createReadStream().pipe() Callback

const stream = fs.createReadStream('<filepath>/example.pdf', {bufferSize: 64 * 1024})
stream.pipe(res);

let had_error = false;
stream.on('error', function(err){
  had_error = true;
});
stream.on('close', function(){
  if (!had_error) fs.unlink('<filepath>/example.pdf');
});

 

posted @ 2017-07-05 16:06  yudis  阅读(2304)  评论(0编辑  收藏  举报