Node JS: Automatic selection of `http.get` vs `https.get`

I have a Node JS app that needs to download a file, given a URL at run-time.

The URL may be either http:// or https://.

How do I best cater for the different protocols?

At the moment I have:

var http = require('http');
var https = require('https');

var protocol = (parsedUrl.protocol == 'https:' ? https : http);
protocol.get(parsedUrl, function(res) {
  ...
});

... but it feels clunky.

Thanks!

There's a bunch of modules you could use instead, like request or needle. They'll figure out which protocol to use, and how to handle redirects (if required) and such.