Skip to content

Instantly share code, notes, and snippets.

@csotiriou
Created May 27, 2020 08:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csotiriou/93822047febd7a5c018d2fab1ffcb8c0 to your computer and use it in GitHub Desktop.
Save csotiriou/93822047febd7a5c018d2fab1ffcb8c0 to your computer and use it in GitHub Desktop.
Proper way of downloading a large file using NodeJS and Axios (NodeJS 10+)
import * as stream from 'stream';
import { promisify } from 'util';
const finished = promisify(stream.finished);
export async function downloadFile(fileUrl: string, outputLocationPath: string): Promise<any> {
const writer = createWriteStream(outputLocationPath);
return Axios({
method: 'get',
url: fileUrl,
responseType: 'stream',
}).then(async response => {
response.data.pipe(writer);
return await finished(writer);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment