How to debug a failing request — Intermediate
Trace the path: client → middleware → router → controller → service → database; read logs and status codes at each step.
Learn: When a request fails, resist random changes—follow the pipeline. Confirm the server port, route match, validation, service/SQL, then global error handler. Logs with request IDs from pino make this faster.
Supplementary examples
Temporary route-level log
router.post("/", (req, res, next) => {
console.log("POST /users body", req.body);
return controller.create(req, res, next);
});Course example
// Typical order to inspect:
// 1. Server running? Correct PORT in .env?
// 2. Request URL and HTTP method match a route?
// 3. Validation middleware rejecting body? (400)
// 4. Service/SQL error? (500 + logs)
// 5. notFound if no route matched (404)
req.log.info({ params: req.params, body: req.body }, "debug");
Additional references & examples
- Express.js guide
Routing, middleware order, and request/response lifecycle.
- JSON formatter (online)
Pretty-print API responses when debugging (used in logging lessons).