Sub topic 1
Anatomy of a URL
Prerequisites
This chapter assumes you can use a web browser and know that pages are written in HTML, CSS and JavaScript. You should also have a basic idea of what an IP address is. To run the code, you need a browser console or Node.js 10 or later.
What a URL is
A URL (Uniform Resource Locator) is a string that tells the browser where a resource lives and how to get it. Before the browser touches the network, it splits the URL into parts. Each part feeds a later stage of the request: the scheme and port choose the protocol, the host goes to DNS, and the path and query go into the HTTP request.
Take this URL:
https://example.com:8443/docs/intro?lang=en&ref=home#setup| Part | Value | Used for |
|---|---|---|
| Scheme | `https` | Picks the protocol, here HTTP over TLS |
| Host | `example.com` | Looked up through DNS to find an IP address |
| Port | `8443` | Selects the service on that machine |
| Path | `/docs/intro` | Names the resource on the server |
| Query | `lang=en&ref=home` | Extra parameters sent to the server |
| Fragment | `setup` | Used only by the browser, never sent |
Parsing a URL in code
The standard URL class does the same splitting the browser does.
const url = new URL('https://example.com:8443/docs/intro?lang=en&ref=home#setup');
console.log(url.protocol); // 'https:'
console.log(url.hostname); // 'example.com'
console.log(url.port); // '8443'
console.log(url.pathname); // '/docs/intro'
console.log(url.search); // '?lang=en&ref=home'
console.log(url.hash); // '#setup'Note that protocol includes the trailing colon, search includes the ?, and hash includes the #.
Details that matter
Default ports
If you omit the port, the browser uses the default for the scheme: 80 for http and 443 for https. The URL class reports an empty string for port when the port is the default. So new URL('https://example.com:443/').port is ''.
The fragment stays local
The browser removes the fragment before it builds the request. The server never sees #setup. The browser uses it afterward to scroll to the element with that id, or JavaScript can read it from location.hash.
The query is a string of pairs
The query is a list of key=value pairs separated by &. The searchParams property parses it for you:
const url = new URL('https://example.com/docs/intro?lang=en&ref=home');
console.log(url.searchParams.get('lang')); // 'en'
console.log([...url.searchParams.keys()]); // ['lang', 'ref']Percent-encoding
URLs may only contain a limited set of characters. Others are written as % followed by two hexadecimal digits. A space becomes %20, and é becomes %C3%A9. The URL class encodes these characters for you when you build a URL from parts.
Common errors
- Missing scheme.
new URL('example.com')throws aTypeError, because without a scheme the string is not an absolute URL. The address bar is more forgiving, which the next chapter covers. - Confusing host and hostname. In the
URLclass,hostincludes the port (example.com:8443), whilehostnamedoes not. - Expecting the fragment on the server. Server code cannot read it, so do not use it for data the server needs.
- Building queries by string concatenation. A value containing
&or=breaks the query. UsesearchParams.set()so the values are encoded.
Exercise
Given http://shop.test/cart?item=42#top, which part is never sent to the server, and which port does the browser use?
Show answer
The fragment #top is never sent. The URL has no explicit port, and the scheme is http, so the browser uses the default port 80.
Exercise
What does new URL('https://example.com/a?x=1&y=2').searchParams.get('y') return?
Show answer
It returns the string '2'. Values from searchParams are always strings, so convert them yourself if you need a number.