DEV Community

Cover image for Check to see if your NodeJs project has a network.
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

Check to see if your NodeJs project has a network.

With two distinct approaches, you may determine whether your web app has connection or not.

no network

Method 1 🥦


require('dns').resolve('www.google.com', function(err) {
    if (err) {
        console.log("No network connection");
    } else {
        console.log("Connected to network");
    }
});

Enter fullscreen mode Exit fullscreen mode

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");
});

Enter fullscreen mode Exit fullscreen mode

Practical Example in a Nodejs

Image description

Result

Image description


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.


Top comments (0)