How do I download a file with Node.js without using third-party libraries?
I don\'t need anything special. I only want to download a file from a giv
Don't forget to handle errors! The following code is based on Augusto Roman's answer.
var http = require('http');
var fs = require('fs');
var download = function(url, dest, cb) {
var file = fs.createWriteStream(dest);
var request = http.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb); // close() is async, call cb after close completes.
});
}).on('error', function(err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message);
});
};
We can use the download node module and its very simple, please refer below https://www.npmjs.com/package/download
Without library it could be buggy just to point out. Here are a few:
Protocol "https:" not supported.
Here my suggestion:
wget
or curl
var wget = require('node-wget-promise');
wget('http://nodejs.org/images/logo.svg');
You can try using res.redirect
to the https file download url, and then it will be downloading the file.
Like: res.redirect('https//static.file.com/file.txt');