Reputation: 5837
I have a Node.js app. I am trying to detect connectivity. I know how to check to see if I can reach the internet by pinging Google. However, I'm curious if there's a way to check to see if I'm connected to a network (i.e. a router) before checking to see if I can access the outside world.
Is there a way I can check to see if I'm connected to a network, but not necessarily get to the outside world? If so, how?
I know in JavaScript, I could check the navigator.onLine
property. However, the navigator
property doesn't exist in Node.
Upvotes: 1
Views: 3375
Reputation: 7464
I would guess your best bet will involve os.networkInterfaces()
, as described in the node.js documentation:
The os.networkInterfaces() method returns an object containing only network interfaces that have been assigned a network address.
Each key on the returned object identifies a network interface. The associated value is an array of objects that each describe an assigned network address.
The properties available on the assigned network address object include: ...
- internal <boolean> - true if the network interface is a loopback or similar interface that is not remotely accessible; otherwise false
So you'll want to look for interfaces where internal
is false.
Upvotes: 3