Downloading Data by Node.js
Takahiro Iwasa
1 min read
Node.js
If we want to download data by Node.js, native Fetch API can be used.
The following is a TypeScript example.
import fs from 'fs';
export async function download(url: string, file: string): Promise<void> {
const response = await fetch(url);
if (!response.ok) {
return;
}
const data = Buffer.from(await response.arrayBuffer());
fs.writeFileSync(file, data);
}