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

Route by Method and Path

In this lesson, you sent your plot board a POST it shouldn’t take, saw it answer 200 OK anyway, and moved its routes into a table so the board checks the path first, then the method.

Key Points

  • Routing on the path alone ignores the method. Every branch compared url.pathname and never req.method, so curl -i -X POST http://localhost:3000/plots got 200 OK and all 24 plots. A POST asks a server to take something in, and the plot list takes nothing in, yet the 200 told the client its POST worked.
  • A wrong path and a wrong method are two different misses. 404 Not Found means no route has this path. 405 Method Not Allowed means the path exists, but its routes don’t take this method, and the response’s Allow header lists the methods they do take.
  • A method check in each branch’s condition isn’t enough. A wrong method would fall through to the fallback 404, and the board couldn’t tell a wrong path from a wrong method.
  • A route table keeps the method, the path, and the handler together. type Route has a method, a path, and a handler. type Handler is (res: ServerResponse, url: URL) => void. Each old branch’s code moves into a handler of its own: sendHello(), sendPlots(), and joinWaitlist(). Only sendPlots() needs the URL, and a function with fewer parameters still fits the Handler type. ServerResponse comes in with import type.
  • The waiting list takes a POST. Joining it sends in a name, so its row is POST /waitlist. For now, joinWaitlist() still answers with the placeholder 404 until you build the waiting list.
  • The listener checks the path first. routes.filter() keeps the matches, the routes whose path is url.pathname. With no matches, no route knows this path, so sendError() sends the 404 and the listener returns.
  • Then it checks the method. matches.find() looks for the match whose method is req.method, and gives undefined when there’s none. Then the path exists but not for this method, so the Allow header gets the matches’ methods, joined with ", ", and sendError() sends a 405 in the error shape.
  • A route that matched on both answers. The listener calls route.handler(res, url).
  • The query string doesn’t change the route. The path check compares the pathname without the query, so GET /plots?vacant=true still reaches sendPlots() and gets 200 OK with plots 4, 13, and 20.

Asked again, the board answers GET /waitlist with 405 Method Not Allowed and Allow: POST, and POST /plots with a 405 and Allow: GET. So the rule is to match the path first, then the method: no route for the path gets a 404, a known path with the wrong method gets a 405 and an Allow header, and otherwise the route’s handler answers.

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 routes on the path now, and it reads the query too. But each branch compares only the pathname, never the method. So send a POST to /plots.

00:12 Curl’s -X option sets the method. The board answers 200 OK, with all 24 plots. The server’s log says POST, but no branch ever asked.

00:25 A POST asks a server to take something in, like a new name for the waiting list. The plot list takes nothing in. Yet the 200 tells the client its POST worked.

00:37 HTTP has a status for this. As a client, you’ve met 404 Not Found, which means no such path. When the path exists but doesn’t take that method, the answer is 405 Method Not Allowed.

00:52 A 405 carries an Allow header listing the methods the path does take. You could add the method to each branch’s condition. But then a wrong method falls through to the 404, and the board can’t tell a wrong path from a wrong method.

01:09 A route table can tell them apart. Each route is an object with a method, a path, and a handler. A handler is a function that gets the response and the parsed URL, and answers.

01:22 Each branch’s code moves into a handler of its own. sendHello() gets the greeting. sendPlots() gets the plot list, with its vacant check.

01:31 joinWaitlist() gets the placeholder. Only sendPlots() needs the URL. sendHello() and joinWaitlist() leave it out, and fewer parameters still fit the Handler type.

01:42 Their response parameters need the ServerResponse type, so import it as a type.

01:48 Then the table lists three routes, one per row: GET for the greeting, GET for /plots, and POST for /waitlist. The waiting list takes a POST, because joining it sends in a name.

02:01 For now, its handler still answers with the placeholder 404, until you build the waiting list.

02:09 Now the listener asks the table, right after it parses the URL. First, filter the routes down to the matches, the ones whose path is the pathname. If there are none, no route knows this path.

02:22 So send the 404, as before, and return. The path goes first because the matches decide what comes next. None means a 404, and their methods fill the Allow header.

02:36 Second, find the match whose method is the request’s method. If there’s none, the path exists, but not for this method. So set the Allow header to the matches’ methods.

02:48 A path with two methods would list both, with a comma. Then send a 405 in the error shape, and return. Otherwise, a route matched on both, so call its handler with the response and the URL.

03:01 Save, and Node restarts the server. Your turn: pause the video and predict what a GET request to /waitlist gets back now.

03:16 It gets 405 Method Not Allowed. /waitlist has a route, so the request passes the first check. That route takes only POST, so the second check fails.

03:28 The Allow header names POST, the method that path takes.

03:32 Now send the POST to /plots again. This time it gets a 405, and the Allow header names GET as the method to use. A query string doesn’t change the route.

03:44 The path check compares the pathname, without the query, and the method check compares GET. Ask for the free plots, and you still reach sendPlots(), with 200 OK and plots 4, 13 and 20.

04:00 So the rule is to match the path first, then the method. No route for the path gets a 404. A known path with the wrong method gets a 405 and an Allow header.

04:12 Your board now routes by method and path. So far, though, every route is one fixed path. In the next lesson, Read a Plot Number From the Path, you’ll answer how big plot 7 is, at /plots/7.

Become a Member to join the conversation.