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

Count the Code You Wrote by Hand

In this lesson, you counted the lines of the plot board you’ve built and sorted them: the lines that decide what the board says, and the lines any server needs to route a request and read its body.

Key Points

  • The board answers all three of Mrs. Okafor’s calls. GET /plots?vacant=true lists the free plots, GET /plots/7 gives one plot’s size, and POST /waitlist adds an entry to the waiting list.
  • As this course wrote it, the board is five files and 250 lines, not counting blank ones. server.ts has 128, body.ts 55, responses.ts 23, plots.ts 17 and waitlist.ts 27. Your own board may differ by a few lines.
  • Routing and parameters take 57 lines, all in server.ts. They’re the Handler and Route types, the routes table, routePath(), and the listener from parsing the URL to calling the handler, with its 404 and its 405 with Allow. Two more lines inside the handlers read vacant from the query string and split the plot number out of the path. Most of this is dispatch machinery, but the route list and the 404 and 405 answers are choices you made.
  • Reading bodies takes 60 lines. All 55 lines of body.ts are there to read one, with every check you built: the 415 for the wrong type, the 413 for a body that’s too large, a client that hangs up, an empty body and broken JSON. Five more lines in joinWaitlist() call readJson() and send its error.
  • Together, that’s 117 of your 250 lines, nearly half. Most of the other 133 are what the board knows and decides: the plots, the entry check in toEntry(), the waiting list file in addToWaitlist(), the error shape in sendError(), and the status each answer gets, like the 404 for a missing plot.
  • Line counts are a rough measure. This board is small, so treat the split as a picture of where your work went, not as a rule about servers in general.
  • Every server needs routing and body reading, and it barely changes between servers. That’s what a framework is for. The next course on the path rebuilds this board with Express, which matches the method and the path and reads JSON bodies for you. You still choose the routes, the limits, such as the size limit, and what each answer says, such as the 415 and the error shape. That’s all you need about frameworks for now.

Because you wrote these lines by hand, you’ll know what a framework is doing for you. Next, in Project: The Hollybank Plot Board, you’ll build Mrs. Okafor’s board from her brief, starting fresh and bringing your helpers along.

00:00 Your plot board now answers all three of Mrs. Okafor’s calls, which plots are free, how big plot 7 is, and how to join the waiting list.

00:10 As this course wrote it, that’s five files and 250 lines, not counting blank ones. Yours may differ by a few. Before the project, count how many of them you’d write again for any server.

00:23 Some decide what the board says. Others get a request to the right handler, or turn its body into data.

00:31 In server.ts, start with routing. The Handler and Route types describe a route. The table lists the board’s four routes, the greeting and Mrs.

00:41 Okafor’s three calls. The routePath() function turns /plots/7 into the table’s pattern.

00:49 Then the listener. It parses the URL, finds the routes for the path, and sends a 404 when there are none. It sends a 405 with Allow when the method doesn’t match, and only then calls the handler.

01:04 Two lines inside the handlers route, too. One reads vacant from the query string. The other splits the plot number out of the path. That’s 57 lines to reach the handler and pull out what the request asked for.

01:19 Next, the body. All of body.ts is there to read one. Every check you built is in it, for the type, the size, a client that hangs up, an empty body and broken JSON.

01:33 Back in joinWaitlist(), five more lines call readJson() and send its error. That’s 60 lines to get from a request to its data. Add them up.

01:45 57 lines route, and 60 read bodies. That’s 117 of your 250, nearly half. Most of the other 133 are what the board knows and decides. They’re the plots, the entry check, the waiting list file, the error shape, and the status each answer gets, like the 404 for a missing plot.

02:08 Line counts are a rough measure, and this board is small. Still, the split shows where your work went.

02:16 Every server needs this work, and it barely changes between servers. That’s what a framework is for. The next course on the path rebuilds this board with Express, a framework that does most of it.

02:28 It matches the method and the path, and reads JSON bodies, for you. You still choose the routes, the limits, and what each answer says. That’s all you need about frameworks for now.

02:40 Because you wrote these lines by hand, you’ll know what a framework is doing for you. First, in Project: The Hollybank Plot Board, you’ll build Mrs. Okafor’s board from her brief, starting fresh and bringing your helpers along.

Become a Member to join the conversation.