# @joshghent/allgood > A health check endpoint for Node.js apps: one handler that reports on the database, cache, disk, memory, CPU and outbound internet, as JSON for monitors or an HTML page for people. Works with Next.js (App Router), Express, Fastify, Hono, and any framework that hands over a Fetch API `Request` and takes a `Response` back. These are instructions for a coding agent adding allgood to an app. Follow them in order. ## 1. Install Use the app's own package manager. Check for a lockfile first — do not guess: - `pnpm-lock.yaml` → `pnpm add @joshghent/allgood` - `yarn.lock` → `yarn add @joshghent/allgood` - `package-lock.json` → `npm install @joshghent/allgood` - `bun.lockb` / `bun.lock` → `bun add @joshghent/allgood` The package name is `@joshghent/allgood`. There is no unscoped `allgood` package — always import from `@joshghent/allgood`. ## 2. Add the route Find the app's framework from package.json and use the matching snippet. Pass the connection strings the app already uses — look for its existing database and Redis environment variables rather than inventing new ones. ### Next.js (App Router) Create `app/api/healthcheck/route.ts` (or `src/app/...` if the app uses a `src` directory): ```ts import { createHealthCheck } from '@joshghent/allgood'; export const runtime = 'nodejs'; export const dynamic = 'force-dynamic'; export const GET = createHealthCheck({ db_connection: process.env.DATABASE_URL, cache_connection: process.env.REDIS_URL, checks: { db_connection: true, cache_connection: true, }, }); ``` - `runtime = 'nodejs'` is required. The checks use Node APIs and database drivers that do not run on the Edge runtime. - `dynamic = 'force-dynamic'` stops Next.js caching one result and serving it forever. - If `next build` fails resolving a database driver, add `serverExternalPackages: ['@joshghent/allgood']` to `next.config`. ### Express, Fastify or Hono ```ts import { createHealthCheck } from '@joshghent/allgood'; app.get('/healthcheck', createHealthCheck({ db_connection: process.env.DATABASE_URL, checks: { db_connection: true }, })); ``` The same line works for all three; allgood detects which one is calling. ### Bun, Deno, Remix, SvelteKit, or any Fetch API handler `createHealthCheck(config)` returns a function that takes a `Request` and resolves to a `Response`. Call it from the route handler: ```ts import { createHealthCheck } from '@joshghent/allgood'; const healthCheck = createHealthCheck({ db_connection: process.env.DATABASE_URL, checks: { db_connection: true }, }); export const GET = ({ request }) => healthCheck(request); // SvelteKit // Bun.serve({ fetch: (req) => healthCheck(req) }) // Bun ``` ## 3. Configuration | Key | Type | Default | Meaning | |---|---|---|---| | `db_connection` | string | — | Database URL. `postgres://`, `postgresql://`, `mysql://` or `mariadb://`. Required when `checks.db_connection` is on | | `cache_connection` | string | — | `redis://…` or `memcached://…`. Required when `checks.cache_connection` is on | | `checks.db_connection` | boolean | `false` | Runs `SELECT 1` | | `checks.cache_connection` | boolean | `false` | Redis `PING`, or memcached `version` | | `dynamodb_table` | string | — | Table name. Required when `checks.dynamodb` is on. Region and credentials come from the AWS SDK default chain | | `checks.dynamodb` | boolean | `false` | `DescribeTable`: `ACTIVE` passes, `UPDATING` warns, anything else fails. Needs `@aws-sdk/client-dynamodb` installed | | `checks.disk_space` | boolean | `true` | Fails under 1 GB free | | `checks.memory_usage` | boolean | `true` | Fails above 80% of the V8 heap limit | | `checks.cpu_usage` | boolean | `true` | Fails above 80%. Takes about a second to sample | | `checks.outbound_internet` | boolean | `true` | Fetches https://1.1.1.1, then https://8.8.8.8 | | `checks.db_migrations` | boolean | `false` | Reserved; not implemented yet | Only turn on checks for things the app actually depends on. If the app has no Redis, leave `cache_connection` off — an enabled check with no connection string fails. ## 4. Response - `200` when every check passes or only warns. `503` when any check fails. Monitors can alert on the status code alone. - Requests with `Accept: text/html` get a status page; everything else gets JSON: ```json { "status": "pass", "description": "Health check response, generated by allgood.", "results": { "db_connection": { "componentName": "db_connection", "status": "pass", "message": "Database connection successful", "value": "true", "time": 12 } } } ``` `status` is `pass`, `warn` or `fail`. For an uptime monitor, check for HTTP 200 or a body containing `"status":"pass"`. ## 5. Do not - **Do not use this as a Kubernetes liveness probe.** A database outage would fail it on every pod at once, and Kubernetes would restart them all. Keep a dependency-free route for liveness; point uptime monitors at this one. - Do not enable `outbound_internet` if the app runs somewhere without internet access, or it will always fail. - Do not cache the response anywhere (CDN or framework): it reports on one moment. ## 6. Shutdown (optional) The database check keeps one single-connection pool per connection string. To close them on shutdown: ```ts import { closeDbClients } from '@joshghent/allgood'; process.on('SIGTERM', async () => { await closeDbClients(); process.exit(0); }); ``` ## Verify Start the app and request the route: ```sh curl -i http://localhost:3000/api/healthcheck ``` Expect `HTTP/1.1 200` and `"status":"pass"`. A `503` names the failing check in `results`.