Created
November 6, 2020 14:19
-
-
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
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
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