Logs, Configuration and Shutdown
A server you can't see into is a server you can't fix. Log in a form tools can search, check every setting when the server starts, shut down without dropping a request, answer a health check, and measure before you try to make anything faster.
Updated
Logs
- Logging with pino (Journeyman): A server's logs are how you find out what happened after it happened, so pino writes each event as one JSON line with a level, pino-http adds a line for every request, redact hides tokens and passwords, and pino-pretty makes the lines readable while you develop.
Configuration and Shutdown
- Configuration Checked at Startup (Journeyman): An API reads every setting it needs in one config module that checks them all with Zod when the server starts, so a missing or mistyped setting stops it at once with a message naming each problem, and NODE_ENV, an .env file and the host's own settings decide the values in each place it runs.
- Graceful Shutdown and Health Checks (Freelancer): A host stops your API with SIGTERM on every deploy, so a graceful shutdown stops taking new requests, lets the ones in progress finish with server.close, then closes MongoDB, and a /health route answers 200 or 503 so the host knows whether to send the API riders.
Performance
- Performance Basics: Measure First (Freelancer): Before making an API faster, measure it, with a timing middleware for one request and autocannon for many at once, because the usual causes of a slow route are CPU work that blocks the event loop, a query with no index, or a list with no paging, and only numbers show which one it is.
Exercise: Shuttle Schedule API
- Exercise: Shuttle Schedule API (Journeyman): Get the campus shuttle's API ready for real riders, with settings checked by Zod at startup, a pino-http log line for every request with tokens and the database address redacted, a /health route that answers 200 or 503, and a graceful shutdown, and let the tests check each one.