npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

http-monitor

v0.3.5

Published

Monitor an http application, and invoke if the server status changes. Both a module and a commandline tool.

Downloads

1,229

Readme

http-monitor build status

Check if a server is running. Is both a module and an executable.

Will report an error if there is the statuscode is 4xx, 5xx or if the server doesn't respond.

Installation

npm install -g http-monitor

Usage (command-line)

With the commandline tool you can set up commands that will be executed when the status of the server changes:

  • The server responds with a 4xx or 5xx error code
  • The server doesn't respond (no reply)
  • The server recovers after some time where it errored

The commands will only be called once if the state of the server changes. Like this scenario:

The server is running perfectly for 3 days, but now stops replying.  
This will cause the on-connection-error to be executed once.  
After 30 minutes the server starts to reply again, and then the on-recovery command is executed once.
http-monitor http://localhost:12345/foo
	--on-http-error "command..."       # When there is a 4xx or 5xx response code
	--on-connection-error "command..." # When there is a connection errror (no reply) from the server
	--on-error "command..."            # When either a connection/http error occur
	--on-recovery "command..."         # When the server recovers after a period with errors
	--interval 5min                    # How often to check
	--retries 4                        # How many times to retry
	--allow 501                        # Allow a 4xx or 5xx code which would otherwise cause an error
	--disallow 301                     # Disallow a 1xx, 2xx, or 3xx code which wouldn't otherwise cause an error
	--once                             # Only run once, then call callback and exit

The "command..." part is a command you want executed when an error occurs. You can use %url, %statuscode, and %body in this. e.g. --on-http-error "call 1234567890 Hi Bill. Server crashed, %url. Returned %statuscode and %body".

Usage (module)

Example

var monitor = require('http-monitor');

monitor('http://localhost:13532/', {
	retries: 1
}).on('http-error', function(err) {
	console.log('The server returned a '+err.statusCode+' statuscode, with the body:'+err.body);
}).on('connection-error', function() {
	console.log('The server could not be reached');
}).on('error', function(err) {
	console.log('This is triggered on both http-error and connection-error');
}).on('recovery', function() {
	console.log('The server just recovered after downtime');
});

Stopping the monitoring

The monitor function returns a stop-function. Call this to stop the monitor.

var monitor = require('http-monitor');

var localhost = monitor('http://localhost:13532/', {
	retries: 1
});

setTimeout(function() {
	// Stop the monitor after 60 seconds
	localhost.destroy();
}, 60000);

Options

interval (miliseconds)

How many miliseconds to wait between the checks. Default is 5000.

retries (integer)

How many tries in a row that should fail before it will call the callback with an error. Default is 1.

timeout (integer)

How many miliseconds should each request maximum take before it is seen as an error. Default is 30000.

allowed (array)

As default http-monitor will call the callback with an error if the server returned a 4xx or 5xx status code. This allows http-monitor to allow certain error codes. [501, 502] would allow the server to return 501 and 502. Default is [].

disallowed (array)

As default http-monitor will call the callback without an error if the server returned a 1xx, 2xx, or 3xx status code. Use this to disallow certain error codes and call the callback with an error. [301, 307] would disallow the server to return 301 and 307. Default is [].

once (bool)

Normally http-monitor will keep pinging the server at the url, but if you set once to true, then it will only happen once and after the callback has been called the first time, it will stop. Default is false.