Last active
December 31, 2019 07:24
-
-
Save paulgalow/9957a5a42e87701a2d41c3f836599d81 to your computer and use it in GitHub Desktop.
Check for internet connectivity using HTTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { parse } = require("url"); | |
function checkHTTP(url) { | |
return new Promise((resolve, reject) => { | |
const { protocol } = parse(url); | |
const lib = protocol === "https:" ? require("https") : require("http"); | |
const request = lib.get(url, response => { | |
console.log(`HTTP Status Code:`, response.statusCode); | |
resolve(response); | |
}); | |
request.on("error", err => { | |
console.error( | |
`Error trying to connect via ${protocol.replace(":", "").toUpperCase()}` | |
); | |
reject(err); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example, how to call that function: