Installed NodeJS on Raspberry Pi, is there a way to check if the rPi is connected to the internet via NodeJs ?
相关问题
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- IPAddress.[Try]Parse parses 192.168 to 192.0.0.168
- google-drive can't get push notifications
- How to reimport module with ES6 import
- Why is `node.js` dying when called from inside pyt
相关文章
- node连接远程oracle报错
- How can make folder with Firebase Cloud Functions
- @angular-cli install fails with deprecated request
- RMI Threads prevent JVM from exiting after main()
- node.js modify file data stream?
- How to resolve hostname to an ip address in node j
- Transactionally writing files in Node.js
- Log to node console or debug during webpack build
A quick and dirty way is to check if Node can resolve
www.google.com
:This isn't entire foolproof, since your RaspPi can be connected to the Internet yet unable to resolve
www.google.com
for some reason, and you might also want to checkerr.type
to distinguish between 'unable to resolve' and 'cannot connect to a nameserver so the connection might be down').As of 2019 you can use DNS promises lookup.
NOTE This API is experimental.
It is very helpful to check internet connection for our browser is available or not.
for more[internet-available][1]: https://www.npmjs.com/package/internet-available
I had to build something similar in a NodeJS-app some time ago. The way I did it was to first use the networkInterfaces() function is the OS-module and then check if one or more interfaces have a non-internal IP.
If that was true, then I used exec() to start ping with a well-defined server (I like Google's DNS servers). By checking the return value of exec(), I know if ping was sucessful or not. I adjusted the number of pings based on the interface type. Forking a process introduces some overhead, but since this test is not performed too frequently in my app, I can afford it. Also, by using ping and IP-adresses, you dont depend on DNS being configured. Here is an example:
It's not as foolproof as possible but get the job done:
just use a simple
if(err)
and treat the response adequately. :)ps.: Please don't bother telling me 8.8.8.8 is not a name to be resolved, it's just a lookup for a highly available dns server from google. The intention is to check connectivity, not name resolution.
I found a great and simple npm tool to detect internet connection. It's looks like more reliable.
First you need to install
npm i check-internet-connected
Then you can call it like follows