Simple function In Node.js to save an image from URL to local disk/ server
Here we are going to see how to Upload an image from URL in Node Js. 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 the server with node.js.
Also, Read Node js interview questions
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'; saveImageToDisk(req.body.profile_pic_url,image_path); }
Also, Read Best Node JS Interview Questions