How to Download Files With Node.js

Downloading files to local storage rather than keeping them in cloud storage has several advantages. These include easier access, access without an internet connection, and complete ownership of your data.

You can download single and multiple files using built-in Node.js modules. You can also use a third-party library instead, to achieve the same results.

4

Downloading Files Without a Third-Party Library

To download a file using Node.js, without the aid of third-party packages, you’ll need three core Node.js modules:fs,https, andpath.

Thefsmodule supports many file tasks including read, create, update, remove, and rename. Thehttpsmodule creates a web server to handle requests and deliver responses. Thepathmodule provides a way to interact with directory and file paths.

two stacks of files and documents, side-by-side

Using these modules, you can send an HTTP GET request to access a file resource from a web server and then make a copy of the resource in your local system.

To begin, import the required modules:

Article image

You will need to use the filename from the URL as a good default name to save it. You can use the path module’s.basename()method to obtain a file’s name from its URL. This method takes a path argument and returns the last portion of a path.

For example:

firefox logo with yellow warning symbol

Then, request the file from the server by invoking thehttps.get()method. This method takes the URL as the first argument and a callback function for the response as the second argument

Pass theurlvariable as the first argument and then a callback function for processing when this method receives a response stream:

iPhone in Landscape View Showing List of Emojis

In the callback function, it’s possible to use thefs.createWriteStream()method to create a new writable stream, passing thefilenamevariable as an argument.

ThecreateWriteStream()method eases the process of writing data to a file, especially when you’re handling large chunks of data.

Thepipe()method then sends the GET response data stream to thefileStreamobject.

To log a message to the console after the script has finished downloading the file, attach a.on()event emitter to thefileStreamvariable:

The fileStream object emits afinishevent when it has written all the data to the file. Catch this via the.on()method and provide a callback function to close thefileStreamand log a message to the console.

For better execution andefficient script reusability, wrap this code in a function that takes the URL as its argument:

To run the function, call it and pass the URL of the file you want to download:

To run the script, open your terminal and enternodefollowed by the name of the JavaScript file:

This script will download the file URL you passed to thedownloadFile()function and save it in your working directory.

Handling Errors When Downloading Files

In Node.js, specific errors like writing to the stream, poor service connection, or issues with the file itself could occur when downloading files. It’s crucial to log error messages when these errors take place to be able to tackle the issue.

Try/Catch Block

A try-catch block is a programming structure that enables you to handle potential errors and exceptions in your code.

The try-and-catch blocks make up the try…catch block. The try block’s code runs first, and the catch block’s code runs if the try block throws an exception.

Use a try/catch block to ensure that you can catch any download-related errors. You can then handle any error as necessary, such as logging it to the console or retrying the download.

HTTP Response Status Code

Status codes for HTTP responses show whether a particular HTTP request has been successfully carried out.

If the request returns a status code outside the success range,200-299, there was a problem with the request. Check the HTTP status code, thenlook up the meaning of the status codeto deal with the error as necessary.

Downloading Multiple Files

you may download multiple files simultaneously by passing the file URLs as arguments when runningnode [script name]. To perform this task, you must modify certain parts of your script.

In JavaScript, arguments passed alongside thenodecommand are available in process.argv, a property of the global Node.js object. This property returns an array of the command line arguments. The first element in this array should benode, the actual command that you run. The second will be the script filename, then each following argument should be a URL.

To download multiple files all at once, save the array fromprocess.argvin a variable. Then run the slice() method to remove the first two elements, that aren’t URLs:

Theslice()method creates a new array from selected elements in an array. This method selects from a specified start to a specified (non-inclusive) end.

In this case, passing a value of 2 removes thenodecommand name and your script filename.

Finally,using JavaScript’s map method, pass each element in theurlsarray todownloadFile():

To run the code, enter thenodecommand along with the name of your JavaScript file and the URLs you want to download:

Using a Third Party Library

You can also download files using a third-party library like the npmdownloadpackage.

Inside your script directory, run the following npm command to install thedownloadpackage:

Require the downloaded package in your script, then save the command-line arguments in an array:

You can now use thedownloadlibrary to download files. You can do this in several ways depending on your particular objective.

Declare thedownloadFilefunction asasync, and create aPromiseto wait until the particular task is complete with theawaitkeyword. Map theurlsarray to thedownloadfunction and pass the name of a local folder—“files”, in this case—to store the files in.

Downloading Files to the Local File System

Node’s built-in functions, like fs and https, make it easy to write a file downloader. To make it even easier, you can use a third-party module like download.

Both methods let you download files from a remote server and save them to the local file system. You should choose the best method for your needs and use it in your next Node.js application.

These tools will let you download entire websites for offline reading, for access even when you don’t have Wi-Fi or mobile internet.

Love fades, even for the best open-source darling.

You’re conveying the wrong meaning when you send these emojis.

Flagship price, mid-range phone.

Unlock a world of entertainment possibilities with this clever TV hack.

I found my TV was always listening—so I shut it down.

Technology Explained

PC & Mobile