Last active
November 11, 2020 15:06
-
-
Save Octalbyte/3826659c20ff3500666383ac7f01d7bb to your computer and use it in GitHub Desktop.
download from url nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const request = require('request'); | |
let download = (url, dest) => { | |
const file = fs.createWriteStream(dest); | |
const sendReq = request.get(url); | |
sendReq.on('response', (response) => { | |
sendReq.pipe(file); | |
}); | |
file.on('finish', () => file.close()); | |
sendReq.on('error', (err) => { | |
throw new Error(err.message) | |
}); | |
file.on('error', (err) => { | |
throw new Error(err.message) | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment