Skip to content

Instantly share code, notes, and snippets.

@EsauPR
Last active June 21, 2017 23:57
Show Gist options
  • Save EsauPR/af77c9a9e29c9197828b98cd6bfa1dee to your computer and use it in GitHub Desktop.
Save EsauPR/af77c9a9e29c9197828b98cd6bfa1dee to your computer and use it in GitHub Desktop.
Download file from nodejs
const http = require('http');
function downloadFile(url) {
return new Promise((resolve, reject) => {
const chunks = [];
const request = http.get(url, response => {
response.on('data', chunk => {
chunks.push(chunk);
});
response.on('end', () => {
const file = new Buffer.concat(chunks).toString('base64');
resolve(file);
});
});
request.on('error', (error) => {
logger.error(error.message);
reject('Can\'t download file');
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment