Skip to content

Instantly share code, notes, and snippets.

@GypsyDangerous
Created November 6, 2020 14:19
Show Gist options
  • Save GypsyDangerous/f55e98be6139d48ab63d08e1652aa65a to your computer and use it in GitHub Desktop.
Save GypsyDangerous/f55e98be6139d48ab63d08e1652aa65a to your computer and use it in GitHub Desktop.
code snippet to download a file from a url in node with typescript
import fs from "fs";
import request from "request"
const download = (uri: string, filename: string, callback: Function) =>{
request.head(uri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
});
};
download('https://www.google.com/images/srpr/logo3w.png', 'google.png', function(){
console.log('done');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment