healthpoint - HTTP Server Health Check

1.0.0 · maintenance · verified Wed Apr 22

healthpoint is a lightweight Node.js utility designed to simplify the exposure of an HTTP server's health status. It provides a straightforward function that returns an HTTP request handler, allowing developers to easily integrate health check endpoints into their applications. The package, currently stable at version 1.0.0, operates on a `require`-based CommonJS module system, indicating a focus on compatibility with established Node.js environments. Its release cadence is likely slow or non-existent, given its simple, self-contained functionality. Key differentiators include its minimal API, ease of integration into existing `http` servers, and the ability to customize both the static properties exposed in the health check JSON and a dynamic asynchronous `check` function for custom health logic (e.g., database connectivity). Unlike more complex monitoring agents, healthpoint provides a focused, on-demand health status report directly from the application.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to set up an HTTP server, integrate `healthpoint` as a middleware for the `/health` endpoint, and implement a custom asynchronous health check function.

const http = require('http');
const healthpoint = require('healthpoint');

// Create a healthpoint handler
// It exposes 'version' and runs a custom check function
const hp = healthpoint({
  version: '1.0.0', // Replace with dynamic version from package.json
  serviceName: 'MyWebApp'
}, function (cb) {
  // In a real application, you would check database connections,
  // external APIs, or other critical services here.
  // For demonstration, we'll alternate between healthy and unhealthy every 5 seconds.
  const isOk = Math.round(Date.now() / 5000) % 2;
  if (isOk) {
    cb(null); // Healthy
  } else {
    cb(new Error('Database connection failed or external service unreachable.')); // Unhealthy
  }
});

// Create a simple HTTP server
const server = http.createServer(function (req, res) {
  if (req.url === '/health') {
    // Delegate health check requests to the healthpoint handler
    return hp(req, res);
  }
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Visit /health for the health check endpoint');
});

server.listen(1337, () => {
  console.log('Server running at http://localhost:1337');
  console.log('Visit http://localhost:1337/health for the health check');
});

view raw JSON →