Last active
December 27, 2017 06:47
-
-
Save thinker3197/ab73654a5deb95a7834d1ad0e52ae76f to your computer and use it in GitHub Desktop.
Download files using 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'), | |
http = require('http'), // Use https module if content is served over https connection | |
const url = 'http://urlToYourFile.com/bla/file.mp4', | |
name = 'filename', | |
extension = 'mp4'; // or mp3, txt, jpeg etc | |
const file = fs.createWriteStream(name+'.'+extension); | |
let request = http.get(url, function(response){ | |
if(response.statusCode === 200) { | |
let stream = response.pipe(file); | |
stream.on('finish', function(){ | |
console.log('Download finished!'); | |
}); | |
} | |
else { | |
console.error('Unable to download file'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment