Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download a file with Node.js using HTTPS?

Tags:

node.js

https

I want to download file from https server using nodejs. I tried this function, but it works only with http:

var http = require('http'); var fs = require('fs');  var download = function(url, dest, cb) {   var file = fs.createWriteStream(dest);   var request = http.get(url, function(response) {     response.pipe(file);     file.on('finish', function() {       file.close(cb);     });   }); }   
like image 724
Ildar Avatar asked Dec 15 '14 11:12

Ildar


People also ask

How do I download a file from HTTP?

In general, downloading a file from an HTTP server terminal via HTTP GET consists of the following steps: Make an HTTP GET request to send to the HTTP server. Send an HTTP request and receive an HTTP response from the HTTP server. Save the contents of the HTTP response file to a local file.

How do I download a .js file from url?

open the Js script link and press ctrl+s i.e save it. Save it by any desired name and then copy it your project folder and then include it in your project files where you have included other files like jquery and css.

How do I download a text file in node?

Method 1: Using 'https' and 'fs' module We can use the http GET method to fetch the files that are to be downloaded. The createWriteStream() method from fs module creates a writable stream and receives the argument with the location of the file where it needs to be saved.


1 Answers

You should use https module then. Quoting the docs:

HTTPS is the HTTP protocol over TLS/SSL. In Node this is implemented as a separate module.

The good news is that the request-related methods of that module (https.request(), https.get() etc.) support all the options that the ones from http do.

like image 80
raina77ow Avatar answered Oct 02 '22 10:10

raina77ow