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

@appsaloon/heartbeat-middleware

v2.2.3

Published

Heartbeat middleware for a proxy service

Downloads

57

Readme

heartbeat-middleware

JavaScript Style Guide

Description

Heartbeat controller for a proxy service.

This middleware can be used to add a GET "/status" endpoint to your Express proxy application.

It will periodically request the status of other services. When the "/status" endpoint is requested, this middleware will respond with either statusCode 200 when all other services are reachable, or 500 when at least one service is unreachable.

Purpose

You can use heartbeat-middleware to add domain.org/status to services like UptimeRobot to make sure you will be notified of connectivity problems within your Express applications.

Usage (Express proxy application)

npm i @appsaloon/heartbeat-middleware

const express = require('express')
const heartbeatMiddleware = require('@appsaloon/heartbeat-middleware')

const app = express()
const options = {
   routes: [
    {url: 'http://service1/status'},
    {
      url: 'http://service2/status',
      dependencies: [
        'http://service1/status',
        'http://service3/status',
      ]
    },
    {url: 'http://service3/status'},
    {url: 'http://service4/status'},
  ],
  interval: 30, // default: 60 seconds
  hideOutput: false, // default: false
}

app.get('/status', heartbeatMiddleware(options))

// the rest of your express proxy app

heartbeatMiddlewareOptions

The options variable passed to HeartbeatMiddleware should be an object with the following properties:

  • routes (required): an array of objects with the following properties:
    • url (required): a string
    • dependencies (optional): an array of strings
  • interval (optional, default 60): a number
  • hideOutput (optional, default false): a boolean

The url of a route will be pinged by the middleware and the client will respond with 200.

If dependencies is provided for a route, the client will ping each dependency and respond with 200 if all dependencies also respond with 200. If any of the dependencies are unreachable, the client will respond with 500.

interval is the duration measured in seconds between calls to the client.

hideOutput can be set to true if you wish to hide the output of the proxy application's "/status" endpoint.

Usage (Express client application)

npm i @appsaloon/heartbeat-middleware

const express = require('express')
const { heartbeatMiddlewareClient } = require('@appsaloon/heartbeat-middleware')

const app = express()
app.get('/status', heartbeatMiddlewareClient)

// the rest of your express client app

No further configuration is required for the client middleware.

Demo

A demo is provided in this repository. The demo contains a docker-compose.yml file with the following services:

  • proxy-service
  • service1
  • service2
  • service3
  • service4

The proxy service is set up to route requests from localhost:3000/service1 to service1, and so on.

The proxy service also has a /status endpoint which will respond with either 200 or 500, depending on whether or not all the services are reachable.

To start the demo, run npm run demo and visit http://localhost:3000/status.

This demo setup has deliberately been broken to show what happens to the output when one or more services become unreachable by heartbeat-middleware. In this case, service1 has been disabled. This will also cause http://service2/status to respond with a 500 status code because service1 has been added to the dependencies array of service2 in the heartbeat middleware options.

To fix the demo, uncomment line 27 in ./demo/docker-compose.yml, and then run npm run demo again:

#    command: node ./src/index.js