Date:

Share:

Downloading files in Node.js with Axios by Josh Sherman

Related Articles

I have used Axios a lot, but have ever used it just to make AJAX requests. In fact, I never remember a time when I had to download a file and save it to disk in Node.js.

That has changed recently with a new side show I helped with. Axios was already part of the stack, and I knew how to make AJAX requests, but the file I had to save locally was a few hundred megabytes.

With the size it is, I did not want to try to load the file in its entirety and then save it on disk, so I started digging with streams.

Streams are the best way to deal with such scenarios, as they allow you to work on smaller chunks instead of on the whole of something. Smaller chunks tend to say less overhead memory which is a good thing, especially if you want to run things in parallel!

Thankfully Axios supports the “stream” type of response, but it was not nearly as simple as I would have liked when I tried to use it with
async/await syntax.

The trick to making it work was promisify God finished Method from
stream So that it can be awaited instead of just plowing into the next block of code.

The finished product looks something like this (with the IIFE added for demonstration purposes):

import * as fs from 'fs';
import * as stream from 'stream';
import axios from 'axios';
import  promisify  from 'util';

(async () => 
  const finishedDownload = promisify(stream.finished);
  const writer = fs.createWriteStream('/path/to/saved/file');

  const response = await axios(
    method: 'GET',
    url: 'https://url/to/remote/file',
    responseType: 'stream',
  );

  response.data.pipe(writer);
  await finishedDownload(writer);
)();

Not much for that and given the lack of complexity, it’s also a pretty obvious code!

good things? Want more?

Weekly emails on technology, development and sometimes sauerkraut.

100% fresh, first-class content, never spam.

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles