Health checks and what happens when nothing matches
A health check is the simplest endpoint a service can offer, and one of the most important. Load balancers, Kubernetes, monitoring tools, and on-call dashboards all rely on a single endpoint that says 'I am alive' to know whether to send traffic to this instance or to take it out of rotation.
The convention is boring on purpose: GET /health, returns 200, body says { status: 'ok' }. Some services add more (database connectivity, cache reachability, version), but always start with the boring version. It is what every external tool expects.
Express handles it for you. If no route matches, Express sends a 404 response automatically. You can customize the 404 with a fallback handler later, but out of the box you get the right behavior. The test suite for this module checks both: /health returns 200, /anything-else returns 404.
A handful of status codes do almost all the work in real APIs: 200 for success, 201 when you created something, 204 for success with no body, 400 when the client sent bad data, 401 when they are not authenticated, 403 when they are authenticated but not allowed, 404 when the thing they asked for does not exist, and 500 when your code blew up. Memorize these and you have most of HTTP.
One route is fine for a demo. Real APIs have dozens of endpoints, organized into resources, with validation, logging, and error handling. Next up you build a full CRUD API for the bookstore using clean layers: routes, controllers, services, and middleware. Same Express, far more shape.
Quiz: Quiz
Loading practice…
Checkpoint: Servers from scratch checkpoint
Loading practice…