In this lesson, you added a placeholder for Mrs. Okafor’s waiting list, watched curl wait for an answer that never came, and made every path through the listener send exactly one response.
Key Points
- A branch that matches but never answers leaves the client waiting. The placeholder for
/waitlistonly loggedWaiting list: coming soon.curl -iprinted no status line, no headers and no body, and kept waiting until you pressed Ctrl+C. - The listener returning doesn’t finish the response. The server’s terminal showed
GET /waitlistand the reminder, so the listener ran and returned, but the response stayed open. Node.js can’t treat a return as done, because a listener often answers later, after it awaits something like a file read. - On your board, every answer is sent and finished by
res.end(). The greeting for/calls it directly,sendJson()calls it for/plots, andsendError()reaches it throughsendJson(). The200 OKyou got without setting a status came from Node.js filling it in whenres.end()sent the response. The placeholder branch never reachedres.end(), so nothing went out, not even a status set withres.statusCode. - The fix is to make the path answer. Until the waiting list is built,
/waitlistanswers with a404in the board’s error shape, sent withsendError(), andcurlgets it right away. You could also delete the branch, because the fallback sends a404too. But the branch marks the spot, and its message says the path isn’t built yet. The fallback’s message can’t say that. - Watch out for a path that answers twice. That happens when a branch sends its answer and code after the
ifsends another. The first response already sent the headers, so whensendError()tries to set them again, Node.js crashes the server withError [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. Once a path has answered, make it stop there, with anelseor areturn.
So the rule is that every path through the listener ends with exactly one response.