Downloading Files with NodeJS
NodeJs gives us native access to the local drives. The code below is an example of how to create a custom module that will download a file via promises. No external libraries are required for this example.
Setup Our Modules
Create a new file called ‘download.js’.
1 | const https = require('https'); |
The ‘http’ and ‘https’ modules are required to make our connection to the site we want to download from.
The ‘fs’ module allows us to access the local file system.
The ‘path’ module allows us to split the URL into useful information.
Setup The Download Function
Below the previous code enter the following:
1 | function download(options){ |
Here we setup the new ‘download’ function. We also gave it a parameter named ‘options’ which represents and object that we will use to submit the options for our download (more on that later).
Additionally, we told this function to return a new promise. If you are not sure exactly how promises work, don’t worry, it’s not critical to making this work.
Setup The Data
Let’s edit the ‘download’ function to look like this:
1 | function download(options){ |
The ‘spec_url’ variable will store our “special” URL. The one that can be parsed by our path
module. We will use this to determine our protocol.
The ‘file_name’ variable will store the name of our file including its file extension.
The ‘file’ variable will store a reference to the actual file will we write.
The ‘return_data’ object will store the data that we will return when the function completes.
Determine Our Protocol
You might have noticed that we needed to import both the ‘http’ and ‘https’ modules. When we want to download a file we need to know if we are going to be accessing it via HTTP or HTTPS so that we can use the right protocol. It would be possible to do this simply by supplying another parameter to the function or adding this to the options object. However we can avoid this (any any potential errors it may cause) by using our special URL we setup earlier.
Go ahead and edit the ‘download’ function to match this:
1 | function download(options){ |
First, you can see we setup some super simple logic to check if the protocol is HTTP or HTTPS. If we can determine the protocol we setup a new ‘protocol’ variable and assign the correct (corresponding) module to it. If we can not determine what protocol we are using, this means we did not supply a valid URL. Here we still resolve the promise, but the ‘return_data’ will still have a value of ‘false’ in the ‘success’ field.
Once we know the correct protocol, we can go ahead and actually download the file, set the ‘success’ field to ‘true’ and resolve our promise with the return data.
We also setup some simple error handling in case something goes wrong.
Final Download.js Code
The final code for the ‘download.js’ file should look like this:
1 | //NodeJS |
Notice that we added an export (at the very bottom) for this function so that we can use it as a custom module.
Using Our Module
Let’s setup a test script so that we can see how our download module actually works.
First we need to make a new directory to store our downloads. Do this now:
1 | #bash |
If you are using Windows just make a new folder called ‘downloads’ in our current directory.
Then make a new file called ‘test.js’ and give it the following content:
1 | const DL = require('./download.js'); |
This slightly more complex example will read a text file full of URLs and try to download each of the pages listed in this file.
1 | const DL = require('./download.js'); |
You can find some URLS to use as your ‘urls.txt’ file on GitHub.
Thanks for reading!