download a file in node js with request code example
Example 1: download file nodejs
const http = require('http');
const fs = require('fs');
const url = 'www.example.com/image.png';
const path = 'app/assets/my_image_name.xlsx'
const request = http.get(url, function(response) {
if (response.statusCode === 200) {
var file = fs.createWriteStream(path);
response.pipe(file);
}
request.setTimeout(60000, function() {
request.abort();
});
});
Example 2: download a file nodejs
const http = require('http');
const fs = require('fs');
const file = fs.createWriteStream("file.jpg");
const request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) {
response.pipe(file);
});