42 lines
1002 B
JavaScript
42 lines
1002 B
JavaScript
const http = require('http')
|
|
|
|
const checkInternetConnection = async () => {
|
|
const options = {
|
|
hostname: 'www.google.com',
|
|
port: 80,
|
|
method: 'HEAD'
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request(options, (res) => {
|
|
if (res.statusCode === 200) {
|
|
resolve(true)
|
|
} else {
|
|
resolve(false)
|
|
}
|
|
req.abort()
|
|
})
|
|
|
|
req.on('error', (err) => {
|
|
resolve(false)
|
|
})
|
|
|
|
req.end()
|
|
})
|
|
};
|
|
|
|
// (async () => {
|
|
// try {
|
|
// const isConnected = await checkInternetConnection()
|
|
// if (isConnected) {
|
|
// console.log('Internet connection is available.')
|
|
// } else {
|
|
// console.log('Internet connection is not available.')
|
|
// }
|
|
// } catch (error) {
|
|
// console.error('Error checking internet connection:', error)
|
|
// }
|
|
// })()
|
|
|
|
module.exports = checkInternetConnection
|