I'm trying to upload images from a url to the server safely.
var http = require('http');
var fs = require('fs');
var path = require('path')
var download = function(url, dest, filename, cb) {
var file = fs.createWriteStream(dest + "/" + filename + path.extname(url));
var request = http.get(url, function(response) {
response.pipe(file);
file.on('error', function(err) {
console.log(err.message);
file.end();
});
file.on('finish', function() {
file.close(cb);
});
}).on('error', function(err) { // Handle errors
fs.unlink(dest);
if (cb) cb(err.message);
});
}
var url = "https://vignette.wikia.nocookie.net/spongebob/images/d/d7/SpongeBob_stock_art.png";
download(url, 'downloadedAssets/imgs', 'spongebob', function onComplete(err) {
if (err) {
console.log(err.message);
} else {
console.log('image uploaded to server');
}
});
Which crashes the server
TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"
I understand I can't upload https files, but why does it crash the server instead of executing file.on('error') then ending the file.
I also tried try/catch and same thing