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

@basementscripts/healthcheck-middleware

v1.0.4

Published

Node.js Express Middleware for healthcheck

Downloads

50

Readme

healthcheck-middleware

Coverage

Node.js Express Middleware for healthcheck

A heathcheck can be a valuable tool in the arsenal of disaster recovery and overall performance. A typical healthcheck is a simple status code 200 "Ok" response from a REST endpoint.

Todays infrastructure, microservices or applications (e.g. Lerna) exsisting of a bundle of microservices, provide a challenge to proper monitoring of services. With that being said, a simple "Ok" means nothing other that the app is standing with a reachable point. In an express app, that is not difficult to achieve.

Well what if you wanted to know the status of a service connection like a Message Queue Broker. Yes you can have a dashboard letting you know on the Broker side what the status of the service is. What if we wanted to have more granular status form an implementing service. How about add a service status to a secured healthcheck? Sounds good to me.

The POST verb should be properly secured behind authentication. Exposing internal configurations to external unsecured audiences can lead to potential security issues. Don't Do It

We've provided a simple ping to handle generic GET request to the healthcheck

usage

Ping

$ curl http://localhost:3001/health

response

Ok

Healthcheck Verbose (requires authentication)

$ curl -X POST http://localhost:3001/health

response

{
	"appName": "API",
	"timestamp": "2020-11-20T23:46:49.251Z",
	"server": 12000,
	"region": "us-east-1",
	"services": [
		{
			"id": "56969530-87f2-4a27-a297-12e4c90b04e9",
			"name": "broker",
			"timestamp": "2020-11-20T23:46:49.252Z",
			"startTime": "2020-11-20T23:46:47.371Z",
			"upTime": "1881",
			"status": "Ok",
			"data": {
				"host": "e-2f0c9slkj-2sj-b4e1-f3b7564asdf3998.mq.us-east-1.amazonaws.com",
				"cluster_name": "broker",
				"platform": "Erlang/OTP 23.0.3",
				"product": "RabbitMQ",
				"version": "3.8.6"
			}
		}
	]
}

This tool has become a vital tool for us, we've decided to share it with you.

Typescript

import { express } from 'express'
import { createServer } from 'http'
import { healthcheck } from '@basementscripts/healthcheck-middleware'

const app = express()

// apply middleware
app.use(
	healthcheck({
		pid: process.pid,
		appName: 'api',
		region: 'us-east-1'
	})
)
const server = createServer(app)

add a service agent to the healthcheck

import { registerStatusAgent } from '@basementscripts/healthcheck-middleware'
import amqp, { Connection } from 'amqplib'

export interface BrokerConnectionOptions {
	name?: string
	host: string
}

export const connect = async ({
	name = 'broker',
	host
}: BrokerConnectionOptions): Promise<void> => {
	const agent = registerStatusAgent({
		name,
		data: {
			host
		}
	})

	try {
		const { connection }: Connection = await amqp.connect(host)
		agent.up(connection.serverProperties)
	} catch (e) {
		agent.down(e)
	}
}

example health check output

{
	"appName": "API",
	"timestamp": "2020-11-20T23:46:49.251Z",
	"server": 12000,
	"region": "us-east-1",
	"services": [
		{
			"id": "56969530-87f2-4a27-a297-12e4c90b04e9",
			"name": "broker",
			"timestamp": "2020-11-20T23:46:49.252Z",
			"startTime": "2020-11-20T23:46:47.371Z",
			"upTime": "1881",
			"status": "Ok",
			"data": {
				"host": "e-2f0c9slkj-2sj-b4e1-f3b7564asdf3998.mq.us-east-1.amazonaws.com",
				"cluster_name": "broker",
				"platform": "Erlang/OTP 23.0.3",
				"product": "RabbitMQ",
				"version": "3.8.6"
			}
		}
	]
}

Authentication

import { express, Request } from 'express'
import { createServer } from 'http'
import { healthcheck } from '@basementscripts/healthcheck-middleware'
import validateAuthToken from '@services/auth/validateAuthToken'

const app = express()

const headhcheckAuthenticationHandler = (headers: Request['headers']) => {
	const token = headers.authorization.replace(/Bearer /, '') // extract bearer token
	return validateAuthToken(token)
}

// apply middleware
app.use(
	healthcheck({
		pid: process.pid,
		appName: 'api',
		region: 'us-east-1',
		authenticate: headhcheckAuthenticationHandler
	})
)
const server = createServer(app)

Extended Configuration

Additional configuration options can be added to the originating healthcheck object

import { express, Request } from 'express'
import { createServer } from 'http'
import { healthcheck } from '@basementscripts/healthcheck-middleware'
import validateAuthToken from '@services/auth/validateAuthToken'

const app = express()

const headhcheckAuthenticationHandler = (headers: Request['headers']) => {
	const token = headers.authorization.replace(/Bearer /, '') // extract bearer token
	if (token) {
		return validateAuthToken(token)
	}
	throw new Error('Unauthorized')
}

// apply middleware
app.use(
	healthcheck({
		pid: process.pid,
		appName: 'api',
		region: 'us-east-1',
		cluster: 'api-ecs-cluster-us-east-1',
		service: 'api-ecs-service-us-east-1',
		authenticate: headhcheckAuthenticationHandler,
		...etc
	})
)
const server = createServer(app)