How Node really works · Chapter 11 of 34
URLs and query strings
By the end of this chapter you can read every part of a link, build links whose values are encoded properly, keep a redirect on your own site, and find a file from a module's URL. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Files and data
- 5 tasks
- Node.js
- In the all labs plan
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.
Why you need this
Every table at Tapri now has a QR code that opens an order link with the table number and the items. Anjali wants menu links with filters that she can share on WhatsApp, and after login the app must send customers back to the page they came from. The server also has to find its data files next to its own code.
Ravi's first version cut links apart with split('?') and split('&'). It broke on an item called "chai & bun", lost an item when the key came twice, and left %20 in the names. Chapter 2 met URL and searchParams.set; this chapter uses the rest of them.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
parseOrderLink: read the table and items from a QR order link
Write parseOrderLink(link). It returns { host, path, table, items }: host is the host with its port when there is one (localhost:3000), path is the path (/order), table is the table parameter as a number, or null when the link has none, and items lists every item parameter, decoded, in order.
Values are decoded whether a space was written as + or %20, and an encoded & stays part of the item (chai & bun). The hash is ignored. A string that is not a link throws a TypeError.
- Task 2
buildMenuLink: a menu link with filters, encoded properly
Write buildMenuLink(base, filters). It returns the full link, as a string: base with one query parameter added per filter, in the order of the filters, after any parameters base already has. An array adds the key once per value, in order. A filter whose value is null or undefined is left out. Any other value becomes text (50, true).
Values are encoded the way URLSearchParams does it, a space as + and & as %26, so a value holding # or ? cannot end the query early. With no filters left, the result is base itself, as the URL class writes it (https://tapri.in/menu stays https://tapri.in/menu).
- Task 3
parseQuery: turn a raw query string into an object
Write parseQuery(query). query is a query string, with or without its leading ?. It returns a plain object with one key per parameter name: a name that appears once maps to its value as a string, and a name that appears more than once maps to an array of its values, in order.
Values are decoded (+ and %20 become spaces, %26 becomes &), an empty value is '', and an empty query gives {}.
- Task 4
safeRedirect: only send customers back to Tapri's own pages
Write safeRedirect(next, origin). origin is Tapri's site, such as https://tapri.in. Resolve next against origin, the way a browser follows a link. When the result has exactly the same origin, return its path, query and hash together (/menu?tag=hot, /order/507#pay, and /account for https://tapri.in/account). Otherwise return '/'.
That covers another site, //evil.com/login, /\evil.com, a host that only starts with tapri.in, another scheme such as javascript: or http: on the same host, and an empty or missing next, which all give '/'. A next that cannot be parsed gives '/' too.
- Task 5
dataFileNear: find a data file next to the script, from import.meta.url
Write dataFileNear(moduleUrl, name). moduleUrl is a module's file URL, what import.meta.url gives. It returns the real file system path of name, taken relative to that module's folder, as a string the fs functions can open.
Folders and files may have spaces in their names (tapri app/data/stock list.csv), and the path must hold real spaces, not %20. name may climb out of the folder (../menu.json).
What you will learn
- The parts of a URL: host, path, query, hash and origin.
- URLSearchParams: reading, building and looping over a query.
- Relative links: resolving against a base, and the redirect trap.
- File URLs: import.meta.url and fileURLToPath.
More in Files and data
Build "URLs and query strings" yourself
Tapri’s order server must never stall at the 4 pm rush. You learn why it would. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of How Node really works are free. This one comes with the all labs plan.