Case No. 003 · Under Investigation
The Express.js Route That Never Answered
A request went in and never came out. Witnesses report a missing next().
The Incident
One API route accepted requests politely, then said nothing. No response, no error, and the client waited until it timed out. The server logs were suspiciously quiet.
The Investigation
Witnesses point to two suspects: a middleware that forgot to call next(), and an async error that nobody caught. Together they let the request vanish without a trace. Exhibit A below shows the suspect in action.
Exhibit A: the suspect
app.get("/orders", auth, async (req, res) => {
const orders = await db.orders.findAll(); // throws? nobody catches it
res.json(orders);
});
function auth(req, res) {
if (!req.headers.authorization) return res.sendStatus(401);
// forgot to call next(), so the request hangs forever
}
The Verdict
A global error handler has been placed at the scene and async routes are now wrapped properly. The case remains open while investigators check for other routes with the same habit. Exhibit B shows the fix.
Exhibit B: the fix
const wrap = (fn) => (req, res, next) =>
Promise.resolve(fn(req, res, next)).catch(next);
function auth(req, res, next) {
if (!req.headers.authorization) return res.sendStatus(401);
next();
}
app.get("/orders", auth, wrap(async (req, res) => {
res.json(await db.orders.findAll());
}));
// global error handler, always registered last
app.use((err, req, res, next) => {
console.error(err);
res.status(500).json({ error: "Something went wrong" });
});