Slip 10 - A) Create a Node.js Application to download jpg image from the Server.

Solution:

download_jpg.js

var fs = require('fs'),

    request = require('request');

var download = function(uri, filename, callback){

  request.head(uri, function(err, res, body){

    console.log('content-type:', res.headers['content-type']);

    console.log('content-length:', res.headers['content-length']);

    request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);

  });

};

download('https://www.google.com/images/srpr/logo3w.png', 'google.png', function(){

  console.log('done');

});

 
Install request module

First, install the request module

C:\Users\Your Name>npm request


Then initiate

C:\Users\Your Name>node download_jpg.js

Post a Comment

0 Comments