Last active
May 22, 2024 16:30
-
-
Save iwstkhr/fdd6e00576e52d55e8f74e24abcf940a to your computer and use it in GitHub Desktop.
Downloading Data by Node.js
This file contains 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'; | |
export async function download(url: string, file: string): Promise<void> { | |
// See https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch | |
const response = await fetch(url); | |
if (!response.ok) { | |
return; | |
} | |
const data = Buffer.from(await response.arrayBuffer()); | |
fs.writeFileSync(file, data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment