With Node.js and fetch how do I determine if the HTTP version of a site is active when it always returns response.ok = true?

Viewed 476

Environment: Node.js, Express, node-fetch

Question: I'm attempting to use node-fetch to determine if a website is active. This works fine for https. However when I test http it always comes back as ok = true, status = 200 & statusText = OK. Even when I use random characters and letters for the url such as qwerweqr12341243124.net it always comes back the same way. Is there a better way to use fetch to determine if the http version is active? Is a different tool required?

Example: The simplified example below checks to see if there is an active http and https version of a website.

const fetch = require('node-fetch');

function checkIfWebsiteExists(website) {

    let httpVersion = `http://${ website }`;
    let httpsVersion = `https://${ website }`;

    fetch(httpVersion)
        .then(function(response) {
            console.log('http ok', response.ok);
            console.log('http status', response.status);
            console.log('http statusText', response.statusText);
        })
        .catch(function(err) {
            console.log(err);
        });

    fetch(httpsVersion)
        .then(function(response) {
            console.log('https ok', response.ok);
            console.log('https status', response.status);
            console.log('https statusText', response.statusText);
        })
        .catch(function(err) {
            console.log(err);
        }); 
}
1 Answers

So, node-fetch by default will automatically follow redirects. So, if the http version of the site is doing a redirect to the https version of the site, then node-fetch will automatically follow that redirect and end up getting you the content from the https version of the site (without telling you).

If you want to not automatically follow the redirect so you can see the real http status that comes back from the http version of the site, then add this option to your node-fetch call:

fetch(httpVersion, { redirect: 'manual' })

Then, when you run your code on a site that does automatic redirection such as www.stackoverflow.com, you will get a 301 status for the http version of the request.

http ok false
http status 301
http statusText Moved Permanently
Related