Check server uptime with Node.js

5

I'm doing multiple HTTP requests at a given time, and I want to check only the return status . There are paths that require login in the application but as it is a very basic script and is not interface test I do not find it necessary to use Zombie.js or Nightwatch.js

Is there a way to make these requests in restricted areas? There is no API, you would need to authenticate login via form

    
asked by anonymous 30.05.2015 / 21:25

1 answer

3
Solution 1

No status code

I am not a Node.JS programmer, but I believe that a ping would solve your problem.

A quick Google search found the net-ping package.

var ping = require ("net-ping");
var session = ping.createSession ();

session.pingHost (target, function (error, target) {
    if (error)
        console.log (target + ": " + error.toString ());
    else
        console.log (target + ": Alive");
});

Note: This solution is not very efficient for your case, because the server may be active, but the service may have dropped.

Solution 2

With status code

I found a package that makes the request and displays the code:

Package Request

var request = require('request');
request('http://pt.stackoverflow.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Mostra o HTML da página inicial do StackOverflow.
  }
});

It is not possible to get only the header or status code in an HTTP request, even though it is not the client that controls this.

The request is made as follows:

  

Client:
  - Hello I want the page index.php
Server:
  - No, here is your page

     
    

Header: Status 200; Etc ...

         

Body index.php page

  
     

Client:
  - Hello, now I want to page akjshlahsdasd.html
Server:
  "Sorry, I did not find it."

     
    

Header: Status 404; Etc ...

         

Body page 404

  
     

Client:
  - Then give me the page serviceInternal.php
Server:
  - Sorry, we're having technical problems

     
    

Header: Status 500; Etc ...

         

Body page 500

  
     

Client:
  - Hmm, give me then data-bank.ini
Server:
  - I'm sorry, you can not access this file

     
    

Header: Status 403; Etc ...

         

Body page 403

  

Note that even for request / server errors the body of an HTML page is returned, this can only be changed in the server's own settings.

Solution 3

A third solution would be to check if the web port is open, but this port may vary on some servers. The default port is 80 , other servers use 8080 .

With the node-portscanner package you can check that the port is open.

> var portscanner = require('portscanner')
undefined
> portscanner.checkPortStatus(443, 'www.google.com', console.log)
> null 'open'
> portscanner.checkPortStatus(80, 'www.google.com', console.log)
undefined
> null 'open'

Note: As in the first solution this is not very efficient, since the server may be active and the port open, but the service may have dropped.

Receiving only server status is only possible if you create an empty page on the server, so you can request it for connection testing, only receiving the request header.

More details about the HTTP protocol:

  • Understanding More About HTTP Protocol
  • HTTP Protocol
  • 30.05.2015 / 21:30