Node Js Axios Download Files (Images / Video) Tutorial

Node js Image and Video File download tutorial; File downloading means transferring data for a specific file; in general, a file could be an image or video file.

In this tutorial, we will show you how to download image files and video files in the Node js application.

Node js is an open-source, cross-platform Javascript runtime environment built on the javascript V8 engine.

It is commonly used for backend and frontend development and is possibly the best platform for creating an event-driven asynchronous application.

To build the download the file feature, we will use the Axios package in node js.

Moreover, we will be using some of the default node js modules such as file system (fs) and path.

How to Download Video and Image Files in Node using Axios

  • Step 1: Generate Folder
  • Step 2: Create Package JSON
  • Step 3: Install Axios Module
  • Step 4: Create and Register App Js
  • Step 5: Test Download Files

Generate Folder

First, step begins with generating a brand new folder.

Type the command onto the command prompt and hit enter, you can also create the folder manually.

mkdir node-demo

Thereafter, you have to move inside the project folder.

cd node-demo

Create Package JSON

For setting up package.json, we will use the npm, init command.

It is used to set up a new or existing npm package. You can also store the project information in this file.

Make sure to execute the given command from the terminal window.

npm init

Install Axios Module

In this step, you have to use the following command for installing the axios library in node js application folder.

npm install axios

Installing nodemon will be beneficial because it restarts app server whenever it detects change in node app files.

npm install nodemon --save-dev

Create and Register App Js

In this step, you require to create an app.js file in your project’s root.

We will be downloading files using command line hence this file needs to be registered into the “main” property in package.json file.

{
  "main": "app.js",
}

Next, you have to create the ‘download’ directory in your project’s root, here you will store the downloaded files.

Now open the app.js file and insert the given code into the file.

const fs = require('fs')
const path = require('path')
const axios = require('axios')

const downloadFileAsync = async (uriFile, directoryDwd) => {
  const nameFile = path.basename(uriFile)

  const pathFile = path.resolve(__dirname, directoryDwd, nameFile)
  try {
    const response = await axios({
      method: 'GET',
      url: uriFile,
      responseType: 'stream',
    })

    const pf = response.data.pipe(fs.createWriteStream(pathFile))
    pf.on('finish', () => {
      console.log('File downloaded successfully :)')
    })
  } catch (error) {
    throw new Error(error)
  }
}

const Video_File_URL =
  'https://remotestack.io/wp-content/uploads/2022/04/pexels-artem-podrez-7049260.mp4'
downloadFileAsync(Video_File_URL, 'download')

const Image_File_URL =
  'https://remotestack.io/wp-content/uploads/2022/03/7019-01.jpg'
downloadFileAsync(Image_File_URL, 'download')

Test Download Files

In the last most step we will test node download file feature.

Make sure to open the terminal and type the given command on the command prompt. Execute the command by pressing the enter button.

nodemon

You may look into the download directory in your node project.

Following will be the output display on your terminal.

[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`

File downloaded successfully :)
File downloaded successfully :)

You will see image file and video file has been downloaded.

Node Js Axios Download Files (Images / Video) Tutorial

Summary

So far, we have learned how to create an asynchronous programme in Node js for downloading the video and image files.

We used the Axios library to build the functionality and created a function that takes file URLs and the directory for storing the files.

We extracted the file names using the path module. More than that, we also took the help of some of the methods provided by Axios and fs packagers.