监听Request pipe 的End事件

由于Request的pipe对象并没有End,Close之类的事件。只能监听pipe目标的状态。

function download (localFile, remotePath, callback) {  
  var localStream = fs.createWriteStream(localFile);

  var out = request({ uri: remotePath });
  out.pipe(localStream);
  localStream.on('close', function () {
    //
    // Now the callback fires :D
    //
    callback(null, localFile);
    console.log('文件下载完成')
  });
};
添加新评论