Downloading Data by Node.js

Downloading Data by Node.js

Takahiro Iwasa
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);
}
Takahiro Iwasa

Takahiro Iwasa

Software Developer at KAKEHASHI Inc.
Involved in the requirements definition, design, and development of cloud-native applications using AWS. Now, building a new prescription data collection platform at KAKEHASHI Inc. Japan AWS Top Engineers 2020-2023.