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

Parse the URL Before Routing

In this lesson, you asked your plot board for Mrs. Okafor’s most common call, the free plots, watched the request miss its branch, and parsed the URL so the board routes on the path and reads the query.

Key Points

  • req.url is the whole request target. For requests like the ones in this lesson, such as /plots?vacant=true, it holds the path and the query string together, as one string. The listener logged GET /plots?vacant=true, and the 404 message named the whole target, Nothing at /plots?vacant=true.
  • Comparing req.url with a path misses any query string. The === operator compares the whole string, so "/plots?vacant=true" matches "/plots" up to the question mark and then keeps going. The request falls past every branch to the fallback, and that happens to any query string, on any path the board knows.
  • The URL class pulls the target apart, given a base. Passed "/plots?vacant=true" alone, new URL() throws TypeError: Invalid URL, because a target like this one is a relative address, and URL can’t read one without a base. The second argument, "http://localhost", is that base. It only fills in the parts the target is missing. Your routes never look at it, so it’s a stand-in.
  • url.pathname holds the path alone, and url.searchParams holds the query. For /plots?vacant=true, url.pathname is "/plots", and url.searchParams.get("vacant") gives the string "true". A name the query doesn’t have, such as "size", gives null.
  • Everything in a URL arrives as text. A query value is always a string, or null when the name is missing, so the board compares vacant with the string "true". With no query, vacant is null, and /plots sends the whole list as before.
  • The ?? "/" after req.url is for the type check. Node.js’s types allow req.url to be undefined, so the ?? gives npm run check a fallback of "/" that your server never uses.
  • A target that URL can’t parse gets a 400. A badly broken target can still make new URL() throw, and an error thrown in the listener stops the server. So the parse sits in a try, with url declared with let above it so the branches below can still read it. The catch sends a 400 Bad Request with sendError() and returns before any routing.
  • The fallback’s message still uses req.url. The branches compare url.pathname, but the 404 message names the whole target the client asked for, query string and all.

So the rule is to parse the URL once, route on its pathname, and read the query from searchParams, where every value comes back as a string, or as null when it’s missing.

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 answers three paths now, and a fallback 404 for the rest. Mrs. Okafor’s most common call asks which plots are free. The finished board answered it at /plots, with the query string ?vacant=true.

00:16 The plot list lives at /plots, so the request should reach that branch. Each branch uses === to compare req.url with a path.

00:27 Your turn: pause the video and predict what curl gets back when it asks for the free plots.

00:35 Curl gets 404 Not Found. The error message names the path and the query string, all of it. Your listener logs req.url, and it holds the request target: the path and the query string together, as one string.

00:52 === compares all of that string with the branch’s path. The two match up to the question mark, and then req.url keeps going. So they aren’t equal, and the request falls past every branch to the fallback.

01:06 That happens to any query string, on any path your board knows. To route on the path alone, you need to pull req.url apart.

01:17 The built-in URL class pulls a URL apart, in Node as in the browser. Try it in the Node REPL first. Pass it the request target on its own, and it throws a TypeError: Invalid URL.

01:31 For requests like these, req.url holds only the path and the query. That’s a relative address, and URL can’t read it without a base. So give it a second argument, a base address:

01:45 "http://localhost". The base only fills in the parts the target is missing. Your routes never look at it, so it’s a stand-in.

01:55 Now the parts are separate. The pathname property holds /plots, the path alone. The searchParams property holds the query.

02:04 Its .get() method gives you the value of vacant as the string "true", in quotes. Ask for a name the query doesn’t have, like size, and you get null back.

02:15 Everything in a URL arrives as text. So a query value is always a string, or null when the name is missing.

02:25 Back in server.ts, parse the URL once, at the top of the listener, right under the log line. Node’s types allow req.url to be undefined, so the two question marks give the type check a fallback your server never uses.

02:41 A badly broken target can still make URL throw, and an error thrown in the listener stops your server. So wrap the parse in a try. Declare url with let above it, so the branches below can still read it.

02:54 In the catch, send a 400 Bad Request with sendError(), and return.

03:02 Then change each branch to compare url.pathname instead of the whole target. The fallback’s message still uses req.url, to name the whole target.

03:13 In the plots branch, read vacant from the query. Compare it with the string "true". When it matches, send the free plots from a new function in plots.ts.

03:24 It keeps the plots with no holder. Import it beside getPlots(). Otherwise, send the whole list, as before. Save, and Node restarts the server.

03:36 Ask for the free plots again. This time, curl gets 200 OK and plots 4, 13 and 20. The log still shows the whole request target. However, the branches now compare only its path.

03:52 Ask for /plots with no query, and vacant is null, so you get all 24 plots.

03:59 So the rule is to parse the URL once, route on its pathname, and read the query from searchParams.

04:08 Your board now answers Mrs. Okafor’s most common call. So far, though, its branches look only at the path. In the next lesson, Route by Method and Path, you’ll make each route check the request’s method too.

Become a Member to join the conversation.