Node.js——流的下载

https://cnodejs.org/topic/59d8f43b2543cb3368b1623e

var request = require('request');
var fs = require('fs');

function downloadFile(uri, filename, callback){
    var stream = fs.createWriteStream(filename);
    request(uri).pipe(stream).on('close', callback); 
}

if (!fs.existsSync('mp4')) {
    fs.mkdirSync('mp4');
}

console.log('Start download...');
for (let i = 1; i <= 22; i++) {
    const filename = 'mp4/NodeJS-lesson-' + i + '.mp4';
    downloadFile('https://cdn.nodelover.me/video_bucket/nodejs/' + i + '.mp4', filename, function() {
        console.log('Success: ' + filename + ' 下载完成!');
    });
}

 

posted @ 2018-07-04 23:14  var_obj  阅读(787)  评论(0编辑  收藏  举报