Sunday, 16 April 2017

Node.js - Input Validation for a URL

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!



via calviners

No comments:

Post a Comment