Is there a way to check if Node.js server is online from html/javascript? When connected I show the messages, but if server is down, nothing happens. Is there a way to do it?
Thanks, Mika
Yes, thanks for the answers, I used 'error' event on socket object to detect server down or outages so I'm assuming that if error is reported, server is down or client has internet connection problems.
In my case, I'm using socket.io.
When using socket.io you have to resquest the socketio.js file to the server and, when this file is loaded it declares the global variable io
If the server is off in the first time you request your file from the tag, you will need to rewrite this tag
var reloadIO = function (){
// if server is not, serveri.io.js will be not available so we need to redeclare de dom node
$("script[src*='socket.io.js']","head").remove();
var script=document.createElement('script');
script.type='text/javascript';
script.src=ioSrc+"?"+Math.floor(Math.random()*111);;
$("head").append(script);
// nullify
script = null;
return;
};
And if you want to check if its loaded :
var isIOLoaded = function(){
return (typeof(io) != "undefined") ? true : false;
}
This way you can have a setInterval checking if io is declared and if it's not, call reloadIO.
Tan's answer is correct, but status code errors do not always state that the server is "down". A 404 status code is simply stating that a document missing -- which is an actual response from your server.
If you are using jQuery, you can send a simple AJAX request and use error
callback. The error
callback will be called when your server return 4xx or 5xx status code. If everything is ok, success
callback is called
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.