Skip to content

Instantly share code, notes, and snippets.

@adityathebe
Last active November 25, 2019 13:59
Show Gist options
  • Save adityathebe/bd7c11d83732d9311361d1402a4dff87 to your computer and use it in GitHub Desktop.
Save adityathebe/bd7c11d83732d9311361d1402a4dff87 to your computer and use it in GitHub Desktop.
Download files with axios.js in Nodejs
/**
* @param {String} uri url of the file to download
* @param {String} filePath File Path
*/
function _downloadFile(uri, filePath) {
return new Promise((resolve, reject) => {
axios({
method: 'get',
url: uri,
responseType: 'stream',
}).then(response => {
const stream = response.data;
stream
.pipe(fs.createWriteStream(filePath))
.on('finish', resolve)
.on('error', reject);
}).catch(reject);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment