I need to write a code that will take an image url on Facebook and download it to the server with nodejs. I used a native nodejs method but it didn't work, because the url of the facebook payload has timestamp at the end, while those tools require the url to end in .jpg or .png.
Url format:https://external-lcy1-1.xx.fbcdn.net/safe_image.php?w=64&url=https%3A%2F%2Fscontent-lcy1-1.xx.fbcdn.net%2Fv%2Ft45.1600-4%2F287436200_23850867680280670_5673356940532891089_n.jpg%3Fccb%3D1-7%26_nc_eui2%3DAeGxRrXd7pkY2ssWbjED_kJ_IqAoXrLmbjcioChesuZuN49f8D6YNKRvBnR55UjJSvQ%26_nc_ohc%3D3fprzn1El7AAX-4AhdH%26_nc_ht%3Dscontent-lcy1-1.xx%26edm%3DAAT1rw8EAAAA%26oh%3D00_AT9zIeMdo8mCWidjpKIH3ni3rF_QSIcf4kKWI4zn5A8OMg%26oe%3D62D2125C&cfs=1&ext=emg0&_nc_eui2=AeH-hxRM-N8xK8-hZD8Tbxpeo9ckW8MrnL-j1yRbwyucv_yxsSs9DC7ahcZ-3T9cfrI&utld=fbcdn.net&_nc_oe=7070b&_nc_sid=58080a&ccb=3-6&_nc_hash=AQFuBfHWEQYiHZdt
I have tried using nodejs native method and libraries, and none of them work.
let fs = require('fs'),
request = require('request');
let 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('url', './fb_ads_images/post.png', function(){
console.log('done');
});