Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Answer Every Request Exactly Once

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 /waitlist only logged Waiting list: coming soon. curl -i printed 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 /waitlist and 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, and sendError() reaches it through sendJson(). The 200 OK you got without setting a status came from Node.js filling it in when res.end() sent the response. The placeholder branch never reached res.end(), so nothing went out, not even a status set with res.statusCode.
  • The fix is to make the path answer. Until the waiting list is built, /waitlist answers with a 404 in the board’s error shape, sent with sendError(), and curl gets it right away. You could also delete the branch, because the fallback sends a 404 too. 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 if sends another. The first response already sent the headers, so when sendError() tries to set them again, Node.js crashes the server with Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. Once a path has answered, make it stop there, with an else or a return.

So the rule is that every path through the listener ends with exactly one response.

The Final Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your plot board sends the greeting for /, the plot list for /plots, and a 404 for every other path. Mrs. Okafor’s third question, how to join the waiting list, gets its own path later in the course.

00:15 Say you mark its place now. You add a branch for /waitlist, right above the else, that only logs a reminder. For this path, the listener logs the reminder and returns.

00:30 When you set no status, Node filled in 200 OK for you. So what does a client get from this branch? Your turn: pause the video and predict what curl shows for /waitlist.

00:49 Curl doesn’t show a status line, a header, or a body. It sits there and waits.

00:58 Yet the server’s terminal shows the request and the reminder. So the listener ran, and it returned. However, returning from the listener doesn’t finish the response.

01:09 Node 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().

01:23 For /plots, sendJson() makes that call. The waitlist branch never gets there, so the response stays open. As for that 200 OK from before, Node fills it in when res.end() sends a response that has no status.

01:39 This branch never calls res.end(), so nothing goes out. Curl keeps waiting for as long as you let it. Press Ctrl+C in the client terminal to stop it.

01:52 The fix is to make this path answer, too. Until the waiting list is built, there’s nothing at this path, so a 404 in the board’s error shape fits. Replace the log line with a message and a call to sendError(), as the fallback does.

02:09 You could also delete the branch, because the fallback already sends a 404. But the branch marks the spot, and its message says this path isn’t built yet.

02:18 The fallback’s message can’t say that. Save, and Node restarts the server. Ask for /waitlist again. This time, curl gets 404 Not Found right away, with the error as JSON.

02:34 Now follow each path through the listener. / ends with the greeting’s own res.end(). /plots ends with sendJson() sending the list.

02:45 /waitlist ends with sendError() sending its 404. Any other path ends with the fallback’s 404. Each path calls res.end() exactly once.

02:57 Watch out for the opposite mistake, too, where a path answers twice. Say a branch sends its answer, and then code after the if sends another.

03:06 The first response already sent the headers, so when sendError() tries to set them again, Node crashes the server. So once a path has answered, make it stop there, with an else or a return.

03:20 So the rule is that every path through the listener ends with exactly one response.

03:26 Your plot board now answers every request it gets, exactly once. In the next lesson, Exercise: Send the Plot List, you’ll write a board that sends Mrs. Okafor her plot list and turns every other request away with a clear error.

03:41 It’s the course’s first coding exercise. You write the server in your browser, and automated tests call it with real requests.

Become a Member to join the conversation.