Request Relay · Level 5 of 7

The dropped baton

Watch what the client gets when one middleware forgets next().

const http = require('node:http');const chain = [  (req, res, next) => { console.log('logger'); next(); },  (req, res, next) => { console.log('auth checks'); /* no next(), no res.end() */ },  (req, res) => { console.log('handler'); res.end('ok'); },];function run(req, res, i) {  chain[i](req, res, () => run(req, res, i + 1));}const server = http.createServer((req, res) => run(req, res, 0));server.listen(0, async () => {  const { port } = server.address();  try {    await fetch(`http://localhost:${port}`, { signal: AbortSignal.timeout(200) });  } catch (err) {    console.log('client: ' + err.name);  }  server.closeAllConnections();  server.close();});

1. Predict the output

Tap the lines in the order you think they print.

  1. 1…
  2. 2…
  3. 3…