Skip to content

Instantly share code, notes, and snippets.

@thinker3197
Last active December 27, 2017 06:47
Show Gist options
  • Save thinker3197/ab73654a5deb95a7834d1ad0e52ae76f to your computer and use it in GitHub Desktop.
Save thinker3197/ab73654a5deb95a7834d1ad0e52ae76f to your computer and use it in GitHub Desktop.
Download files using NodeJs
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