Node.js Health Checks
A process can be running while it should not receive normal traffic. A readiness endpoint communicates a bounded operational state instead of exposing sensitive configuration or pretending every dependency is healthy.
How it works
This local fixture starts unready, then changes only its own readiness flag and checks both statuses through real HTTP requests. A real service may base readiness on startup completion and the specific dependencies needed for its work, with timeouts and failure policy. Do not make every probe perform an expensive database query or return credentials and internal topology to an unauthenticated client.
Run the code
Run this example locally with Node.js. The exact sources are reviewed on Node 22.15.1 and Node 24.22.1 unless a specialised lab states otherwise. Save the complete source as readiness.mjs in a separate empty exercises folder and run node readiness.mjs. There is no Node.js browser runner on Bhylu; the plain JavaScript playground does not provide these filesystem, server or process APIs.
Download the complete source. Ordinary Node scripts need no Apache, PHP or MySQL service. File examples own their temporary folders; network fixtures use loopback and available temporary ports.
Example
import http from 'node:http';
import { once } from 'node:events';
let ready = false;
const server = http.createServer((request, response) => {
if (request.url !== '/ready') return response.writeHead(404).end();
response
.writeHead(ready ? 200 : 503, { 'content-type': 'text/plain' })
.end(ready ? 'ready' : 'not ready');
});
server.listen(0, '127.0.0.1');
await once(server, 'listening');
const base = 'http://127.0.0.1:' + server.address().port;
try {
let response = await fetch(base + '/ready');
await response.text();
console.log(response.status);
ready = true;
response = await fetch(base + '/ready');
await response.text();
console.log(response.status);
} finally {
server.closeAllConnections();
await new Promise((resolve, reject) =>
server.close((error) => (error ? reject(error) : resolve())),
);
}
Expected result
503 200
The service changes its readiness response without restarting the process. This tests the endpoint’s contract only; it does not claim that a cloud load balancer, container orchestrator or public reverse proxy has been configured correctly.
Use the Node.js API reference for the precise version contract.