0

I'm currently using the node module 'request' to create a job queue that fetches HTML data from a URL that a user inputs. This is my code so far:

var jobQueue = [];

function processNextJob() {
    if (jobQueue.length < 1) {
        console.log("No jobs to complete");
    }
    else {
        var job = jobQueue[0];
        request(job.target , function (error, response, body) {
            console.log(body);    
        });
        jobQueue.shift();
    }
}

The code works if I type things like "http://www.google.com" or "http://facebook.com", but not if I type only "google.com" or "twitter.com". What would the best way for me to make an http request for any valid link the user types into the form? For example, I want it to work when a user types just "google.com" or "www.google.com" without the "http:// at the beginning.

Any ideas? Thanks in advance for the help!

calviners
  • 137
  • 1
  • 2
  • 10
  • You could use regex to check if `http://` or `https://` is part of the beginning of the users input, and if it is not, just add it to the left side of the string of the users input. – Webeng Apr 17 '17 at 06:37
  • You can take look at `url` module of the node. https://nodejs.org/api/url.html (latest node v7.9.0) – hisener Apr 17 '17 at 06:39

1 Answers1

0

One possible solution is to check if the url has "http://" on the beginning and prepend it if it does not. For example:

var prefix = 'http://';
if (s.substr(0, prefix.length) !== prefix) {
  s = prefix + s;
}

As a note, I would recommend formatting the strings when they are input rather than when they are processed. That way, you don't have check if they are properly formatted again if you use them in a different function.

Source

Community
  • 1
  • 1
Chris Thorsvik
  • 450
  • 7
  • 15