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 iret, Inc.
Architecting and developing cloud native applications mainly with AWS. Japan AWS Top Engineers 2020-2023