URL and URLSearchParams
Local practice: Node.js 22.16.0 and npm 10.9.2 on Linux x64; proposed teaching baseline 26.13.1 LTS remains untested. Use a fresh disposable folder for each example, copy only the shown files and run the fixed command. No packages or external services required. Static examples are not ByteMind runner support. The archive is author-only; do not copy expected.json into the example directory.
Construct a catalog link
import { URL } from "node:url";const url = new URL("/catalog", "https://example.test/shop/");url.searchParams.set("q", "tea & books");url.searchParams.append("tag", "new");url.searchParams.append("tag", "sale");console.log(url.href);console.log(JSON.stringify(url.searchParams.getAll("tag")));Run in this fixture directory:
node main.mjsExpected stdout:
https://example.test/catalog?q=tea+%26+books&tag=new&tag=sale["new","sale"]Expected exit status: 0. Expected stderr: empty. A leading slash selects the root path; the query serializer encodes the ampersand as data. No request is made.
How it works
new URL(input, base) can resolve relative references; a complete absolute input can override the base. pathname represents the URL path, origin identifies scheme and host/port for HTTP(S), and searchParams exposes query fields. get returns the first matching value or null; getAll returns every match. set replaces existing values for a name, while append adds another. Values are strings and are encoded when serialized. URL parsing is not network access and does not authorize requests. For actual fetches later, validate allowed destinations and redirects separately. For filesystem paths use fileURLToPath/pathToFileURL rather than manipulating file URL strings by hand.
Distinguish empty and absent
import { URLSearchParams } from "node:url";const params = new URLSearchParams("q=&tag=old&tag=new");console.log(JSON.stringify([params.get("q"), params.get("page"), params.get("tag")]));params.set("tag", "featured");console.log(params.toString());Run in this fixture directory:
node main.mjsExpected stdout:
["",null,"old"]q=&tag=featuredExpected exit status: 0. Expected stderr: empty. Empty is an empty string, missing is null, and set replaces repeated tag entries.
Common mistake
Do not concatenate raw search text after ?q=. A value containing & could be mistaken for another field. Do not assume a base URL confines absolute input to that origin.
Try it yourself
Build a catalog URL with a search term containing spaces and an ampersand, and two tag values. Retrieve both tags; distinguish absent from empty query values.
Further reading: Official documentation 1. Related ByteMind reference candidate: node-url-searchparams-getall (route not verified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
URL and URLSearchParams — knowledge check
Apply the concepts and predict the behavior taught in this lesson.
Which method returns every value for a repeated query name?
What does get return for a missing name?
Does new URL(input, base) enforce the base origin?