Skip to content
engineering workbench.by StudyPort Academy
All errors
async · DEBUG LIBRARY

The request hangs because next was never called

Locate the failed assumption, reproduce it, then repair the cause.

What actually caused it

A middleware that neither calls next nor writes a response leaves the request open. Express does not guess that you are finished. The client waits, and this runner stops the process at the three second limit. The route below the middleware is not wrong. It never started. The same hang happens when an function returns a and you forget to call next after the await, or you return early on a branch that does not send a response. The missing call is invisible in the status line because no status was sent.

Minimal reproduction

Neither next nor a response

app.use((req, res, next) => {
  const ok = true;
  if (!ok) res.status(401).send('no');
});
app.get('/', (req, res) => {
  res.send('ok');
});

When ok is true, the request never moves and never ends.

The fix

On every path through the , either call next or send the response. After an await, next still has to be called if you want the route to run. If the function is rejecting the caller, send a status and a body and do not also call next. Then run one request. A timeout means a path still does neither. A response means that path finished the cycle.

Call next when the check passes

app.use((req, res, next) => {
  const ok = true;
  if (!ok) {
    res.status(401).send('no');
    return;
  }
  next();
});

The passing path hands the request to the route.