With two distinct approaches, you may determine whether your web app has connection or not.
Method 1 🥦
require('dns').resolve('www.google.com', function(err) {
if (err) {
console.log("No network connection");
} else {
console.log("Connected to network");
}
});
Method 2 🥦
First, install the package npm install internetAvailable
const internetAvailable = require("internet-available");
internetAvailable().then(function(){
console.log("Internet available");
}).catch(function(){
console.log("No internet");
});
Practical Example in a Nodejs
Result
Conclusion
If you choose, you can alternatively send the response to the Front End. I hope you found this article useful.
Thank you for taking the time to read this, and may the Node force 👨🏾 be with you.