Node.js request directly to URL with options (http or https)

http, https, node.js

Solution

In order to get all components out of URL you need to parse it. Node v0.10.13 has stable module for it: `url.parse`

This is simple example how to do so:

var q = url.parse(urlStr, true);
var protocol = (q.protocol == "http") ? require('http') : require('https');
let options = {
    path:  q.pathname,
    host: q.hostname,
    port: q.port,
};
protocol.get(options, (res) => {...

Problem

I think I'm missing something about http and https requests I have a variable that contains a URL, for example: `http(s)://website.com/a/b/file.html` I would like to know if there's a easy way to make a request to that URI to get the data To make a http(s)Request, here's what I have to do now: - Test if the URL is http or https to make the appropriate request - Remove the http(s):// part and put the result in a variable (If I specify http or https in the hostname, I get an error) - Separate the hostname from the path: `website.com` and `/a/b/file.html - Put this variables in the options objects Is this a must or are they easier solutions that don't involve getting out the hostname and path, and testing if the site is in http or https ? Edit: I can't use http.get as I need to put some specific options

Original source