2

Check if the node.js server is online?

 3 years ago
source link: https://www.codesd.com/item/check-if-the-node-js-server-is-online.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Check if the node.js server is online?

advertisements

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK