Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AliAryanTech/982dee98c423f38e3e7c7e5bdef9eec5 to your computer and use it in GitHub Desktop.
Save AliAryanTech/982dee98c423f38e3e7c7e5bdef9eec5 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