Simple function In Node.js to save image from url to local disk/ server

Online Interview Questions
1 min readFeb 21, 2018

--

Whenever we use login with facebook,google or any other social media in node js app, we get some basic information like name, email and profile picture from there. In this post i will show you how to save google, facebook profile picture to server with node.js

var fs = require('fs');
var https = require('https');
//Node.js Function to save image from External URL.
function saveImageToDisk(url, localPath) {var fullUrl = url;
var file = fs.createWriteStream(localPath);
var request = https.get(url, function(response) {
response.pipe(file);
});
}

Usages

exports.saveImage(req, res) {let image_path='./uploads/profile/'+Date.now()+'.jpg';
fetchImage(req.body.profile_pic_url,image_path);
}

Article Published first on https://www.onlineinterviewquestions.com/uploading-image-url-using-node-js/

--

--