express-healthcheck-endpoints
Version:
Basic health check provider for Express.js applications. It allows you to register health checks and provides custom ways to retrieve the health status of your application.
53 lines • 1.74 kB
JavaScript
import { healthStatusValues, healthStatusCodes } from "./healthEnums.js";
class HealthCheckRegistry {
constructor() {
this.checks = new Map();
}
register(name, healthCheck) {
if (this.checks.has(name)) {
throw new Error(`Health check with name '${name}' is already registered.`);
}
this.checks.set(name, healthCheck);
}
getResults() {
return Array.from(this.checks.entries()).map(async ([name, check]) => ({
[name]: await check.mapToObject()
}));
}
getOverallStatusResponse(healthChecks) {
// Determine overall status
const areAllUnknown = healthChecks.length === 0 || healthChecks.every(r => r.isUnknown());
const hasUnhealthy = healthChecks.some(r => r.isUnhealthy());
let overallStatus = healthStatusValues.healthy;
let overallStatusCode = healthStatusCodes.healthy;
if (areAllUnknown) {
overallStatus = healthStatusValues.unknown;
overallStatusCode = healthStatusCodes.unknown;
} else if (hasUnhealthy) {
overallStatus = healthStatusValues.unhealthy;
overallStatusCode = healthStatusCodes.unhealthy;
}
return {
overallStatus,
overallStatusCode
};
}
async handler() {
return async (_, res) => {
const arrResults = await Promise.all(this.getResults());
const results = Object.assign({}, ...arrResults);
const healthChecks = Array.from(this.checks.values());
const {
overallStatus,
overallStatusCode
} = this.getOverallStatusResponse(healthChecks);
return res.status(overallStatusCode).json({
overallStatus,
overallStatusCode,
uptime: process.uptime(),
results
});
};
}
}
export default HealthCheckRegistry;