node:url
The node:url module in Azion Runtime: URL parsing and formatting, examples for request URLs, query parameters, validation, and routing, and the supported APIs.
The node:url module provides the Node.js utilities for resolving, parsing, and formatting URLs. With it, a function reads the parts of a URL in a structured way, changes them, and builds URLs for routing and API requests. Azion Runtime provides the module through its Node.js compatibility. A function can use it to parse the incoming request URL, read its query parameters, and build URLs when it proxies or redirects traffic. In Azion Runtime, url.URL is the same object as the global URL, and the legacy functions url.parse(), url.format(), and url.resolve() work in a deployed function.
Examples
Each example is a complete function that imports url from node:url. The response below each example is the one a deployed function returns.
URL parsing and formatting
This function creates a URL with the global URL class, builds a URL string from an object with url.format(), and parses that string with url.parse():
The function responds with Done!. It logs the URL, the deprecation warning that url.parse() prints, and the parsed object:
Request URL parsing
This function reads the components of the request URL, collects its query parameters, and splits its path into segments:
For a GET request to https://example.com/blog/post-1, the function responds with the URL components, an empty query, and the two path segments:
Dynamic URL construction
This function builds an API URL with query parameters, a redirect URL with url.format(), and a URL from its parts. It also resolves a relative path twice, with url.resolve() and with the URL constructor:
The function responds with the four URLs. Both ways of resolving the relative path log https://example.com/api/reference, and the URL built from parts keeps the port 443:
Query parameter handling
This function reads, checks, changes, and sorts the query parameters of the request, then builds a second query string from an object:
For a request without a query string, the function responds with the default values, the changed parameters, and both query strings. The timestamp value comes from Date.now():
URL validation and normalization
This function checks a list of URLs with the URL constructor, then normalizes each valid URL: it lowercases the hostname and removes a default port:
The function responds with the result for each URL and the normalized list. The URL constructor rejects not-a-url with the message Invalid URL: 'not-a-url', and the normalized list holds null in its place:
URL routing and path matching
This function matches the request path against a route table, including a route with an :id parameter, and checks whether the route accepts the request method. It answers 404 when no route matches, and 405 when a route matches but its methods do not include the request method:
For a GET request to https://example.com/blog/post-1, a path the route table does not list, the function takes its no-match branch and responds with status 404 and this body:
Supported APIs
The table lists the status of each node:url API in Azion Runtime:
| API | Status |
|---|---|
URL (global) | 🟢 Supported |
URLSearchParams | 🟢 Supported |
url.URL | 🟢 Supported |
url.URLSearchParams | 🟢 Supported |
url.format() | 🟢 Supported |
url.parse() | 🟡 Supported (deprecated, use new URL()) |
url.resolve() | 🟡 Supported (deprecated, use new URL(relative, base)) |
url.domainToASCII() | 🟡 Partially supported |
url.domainToUnicode() | 🟡 Partially supported |
url.fileURLToPath() | 🟡 Partially supported |
url.pathToFileURL() | 🟡 Partially supported |
url.urlToHttpOptions() | 🟡 Partially supported |
APIs marked 🟡 Partially supported have limited functionality compared to the full Node.js implementation. In a deployed function, url.fileURLToPath("file:///tmp/x.txt") returns /tmp/x.txt, url.pathToFileURL("/tmp/x.txt") returns file:///tmp/x.txt, and url.domainToASCII("español.com") returns xn--espaol-zwa.com.
A function that calls url.parse() can log this deprecation warning, as the first example shows:
Node.js deprecates url.parse(), url.resolve(), and url.format() from v11.0.0. Use the WHATWG URL and URLSearchParams APIs instead: in Azion Runtime, both are globals that need no import.