Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Last active April 7, 2021 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazy4groovy/7afd0ed9fbb2f663e695ff38a32da26f to your computer and use it in GitHub Desktop.
Save crazy4groovy/7afd0ed9fbb2f663e695ff38a32da26f to your computer and use it in GitHub Desktop.
fetch / download a URL / binary file (NodeJS)
const fs = require("fs");
const fetch = require("node-fetch");
function dl(url, destFilename) {
return new Promise((resolve, reject) => {
fetch(url).then((res) => {
res.body.pipe(fs.createWriteStream(destFilename))
res.body.on('end', resolve)
res.body.on('error', reject)
}).catch(reject)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment