How to check if a url is reachable in javascript?

I have a .js file from which I am assigning several URL addresses that are then being accessed in the frontend, i.e.

let serviceURL1 = “http:/….com”
let serviceURL2 = “http:/….com”

This script is loaded in to the HTML frontend and serviceURL1 is accessed on button click.

How can I check if each of these is reachable, and if not, assign an alternative URL?

The approach I’m trying is like this:

const https = require('https')
https.get('https://avgmarkingress.40260285.qpc.hal.davecutting.uk/', res => {
    if(res.statusCode === 200) {
        console.log('Service online.')
        avgMarkURL = "http://avgmarkingress.40260285.qpc.hal.davecutting.uk/";
    } else {
        console.log('Service offline. Redirecting to backup URL.')
        avgMarkURL = "http://serviceURLbackup.uk/";
    }
})

But it doesn’t seem to be working and I don’t really know what I’m doing to be honest, I appreciate any help from anyone.