javascriptnode.jsexpressrequestjs

Multiple Website Status Check with Node JS


I want to run a node app that would check multiple websites and responds with the proper status code. I am using the 'request' module of Node itself.

The code I have is:

const request = require('request')

function getStatus() {
    request('https://www.google.com', function(error, response, body) {

        result = response.statusCode;

        if(!error && response.statusCode == 200) {
            document.write("The Site Is Up");
            console.log(result);
        } else {
            console.log("The Site Is Down");
        }
    });
}

This works fine for one website. But, I want to know how can I run the same function for multiple sites? For example, the url should be fed from an Array of urls.

If I have an array of urls such as:

var urls = ["https://www.google.com", "https://www.yahoo.com"];

How can I feed these individual urls to the request function? and get results for each site?


Solution

  • You can do this with a list of promises, should be easy enough to do:

    const request = require('request')
    
    const urlList = ["https://www.google.com", "https://www.amazon.com"];
    
    function getStatus(url) {
        return new Promise((resolve, reject) => {
            request(url, function(error, response, body) {
                resolve({site: url, status: (!error && response.statusCode == 200) ? "OK": "Down: " + error.message});
            });
        })   
    }
    
    let promiseList = urlList.map(url => getStatus(url));
    
    Promise.all(promiseList).then(resultList => {
        resultList.forEach(result => console.log("Result: ", result));
    });
    

    You can also us the request-promise-native library for a nicer API:

        const request = require('request')
        const rp = require('request-promise-native')
    
        const urlList = ["https://www.google.com", "https://www.amazon.com", "https://doesnotexist.none", "https://wikipedia.org"];
    
        function getStatusRp(url) {
            return rp({uri: url, resolveWithFullResponse: true }).then((response) => {
                return { site: url, status: response.statusCode === 200 ? "OK": "Down"};
            }, (err) => {
                return { site: url, status: "Down: " + err.message};
            });
        }
    
        let promiseList = urlList.map(url => getStatusRp(url));
    
        Promise.all(promiseList).then(resultList => {
            resultList.forEach(result => console.log("Result: ", result));
        });