Last active
March 19, 2020 09:50
-
-
Save victorchee/72f7009656b7eb0a888db4f1586a5b6c to your computer and use it in GitHub Desktop.
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 path = require('path'); | |
const fetch = require('node-fetch'); | |
if (!fs.existsSync('images')) { | |
fs.mkdirSync('images'); | |
} | |
async function download(url) { | |
const request = await fetch(url); | |
const filePath = './images/' + path.basename(url) | |
const stream = fs.createWriteStream(filePath); | |
return new Promise((resolve, reject) => { | |
request.body.pipe(stream); | |
request.body.on('error', error => { | |
reject(error); | |
}); | |
stream.on('finish', () => { | |
resolve(); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment