Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Download a txt File from a URL using Node JS

I would like to use node to download the .txt file from this link:

https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt

and save it into the same directory as my .js file.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

After doing some research it seems most answers here on stack overflow use the request method which is now deprecated.

What is the best way to do this without using 3rd party libraries?

>Solution :

You can use https module:

const https = require('https');
const fs = require('fs');
const url = 'https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt';

https.get(url, (response) => {
  const file = fs.createWriteStream('http.txt');

  response.on('data', (chunk) => {
    file.write(chunk);
  });

  response.on('end', () => {
    console.log('File downloaded successfully');
    file.end();
  });

}).on('error', (err) => {
  console.error('Error: ', err.message);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading