Using URLs in web applications
Let’s take a look at the following table, made by Node.js, that describes the different parts of a URL:
Figure 9.9 – URL structure from the Node.js Official Documentation. Attributions and copyright licensing by Node.js Contributors is licensed under MIT
In the following chapters, we will use the URL parts a lot, so keep this table handy. There are many ways to parse a URL, but the most common way is to use the URL
class:
import { URL } from 'node:url'; const myUrl = new URL('https: //user:pass @sub.example. com:8080 /p/a/t/h?query=string#hash'); console.log(myUrl.hash); // #hash console.log(myUrl.host); // sub.example.com:8080 console.log(myUrl.hostname); // sub.example.com
This class is available in Node.js and in the browser.
Important info
Node.js 20 introduced one of the most performant URL parsers available, called Ada 2.0: https://www.yagiz.co/announcing-ada-url-parser-v2...