Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get download progress in Node.js with request

I'm creating an updater that downloads application files using the Node module request. How can I use chunk.length to estimate the remaining file size? Here's part of my code:

var file_url = 'http://foo.com/bar.zip'; var out = fs.createWriteStream('baz.zip');  var req = request({     method: 'GET',     uri: file_url });  req.pipe(out);  req.on('data', function (chunk) {     console.log(chunk.length); });  req.on('end', function() {     //Do something }); 
like image 542
Jack Guy Avatar asked Aug 19 '13 21:08

Jack Guy


1 Answers

This should get you the total you want:

req.on( 'response', function ( data ) {     console.log( data.headers[ 'content-length' ] ); } ); 

I get a content length of 9404541

like image 64
fakewaffle Avatar answered Oct 05 '22 15:10

fakewaffle