99The ` url ` module provides utilities for URL resolution and parsing. It can be
1010accessed using:
1111
12- ``` js
12+ ``` mjs
13+ import url from ' url' ;
14+ ```
15+
16+ ``` cjs
1317const url = require (' url' );
1418```
1519
@@ -61,7 +65,13 @@ const myURL =
6165
6266Parsing the URL string using the Legacy API:
6367
64- ``` js
68+ ``` mjs
69+ import url from ' url' ;
70+ const myURL =
71+ url .parse (' https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash' );
72+ ```
73+
74+ ``` cjs
6575const url = require (' url' );
6676const myURL =
6777 url .parse (' https://user:pass@sub.example.com:8080/p/a/t/h?query=string#hash' );
@@ -135,7 +145,12 @@ const myURL = new URL('/foo', 'https://example.org/');
135145The URL constructor is accessible as a property on the global object.
136146It can also be imported from the built-in url module:
137147
138- ``` js
148+ ``` mjs
149+ import { URL } from ' url' ;
150+ console .log (URL === globalThis .URL ); // Prints 'true'.
151+ ```
152+
153+ ``` cjs
139154console .log (URL === require (' url' ).URL ); // Prints 'true'.
140155```
141156
@@ -573,10 +588,6 @@ and [`url.format()`][] methods would produce.
573588The ` toString() ` method on the ` URL ` object returns the serialized URL. The
574589value returned is equivalent to that of [ ` url.href ` ] [ ] and [ ` url.toJSON() ` ] [ ] .
575590
576- Because of the need for standard compliance, this method does not allow users
577- to customize the serialization process of the URL. For more flexibility,
578- [ ` require('url').format() ` ] [ ] method might be of interest.
579-
580591#### ` url.toJSON() `
581592
582593* Returns: {string}
@@ -931,8 +942,20 @@ invalid domain, the empty string is returned.
931942
932943It performs the inverse operation to [ ` url.domainToUnicode() ` ] [ ] .
933944
934- ``` js
945+ ``` mjs
946+ import url from ' url' ;
947+
948+ console .log (url .domainToASCII (' español.com' ));
949+ // Prints xn--espaol-zwa.com
950+ console .log (url .domainToASCII (' 中文.com' ));
951+ // Prints xn--fiq228c.com
952+ console .log (url .domainToASCII (' xn--iñvalid.com' ));
953+ // Prints an empty string
954+ ```
955+
956+ ``` cjs
935957const url = require (' url' );
958+
936959console .log (url .domainToASCII (' español.com' ));
937960// Prints xn--espaol-zwa.com
938961console .log (url .domainToASCII (' 中文.com' ));
@@ -956,8 +979,20 @@ domain, the empty string is returned.
956979
957980It performs the inverse operation to [ ` url.domainToASCII() ` ] [ ] .
958981
959- ``` js
982+ ``` mjs
983+ import url from ' url' ;
984+
985+ console .log (url .domainToUnicode (' xn--espaol-zwa.com' ));
986+ // Prints español.com
987+ console .log (url .domainToUnicode (' xn--fiq228c.com' ));
988+ // Prints 中文.com
989+ console .log (url .domainToUnicode (' xn--iñvalid.com' ));
990+ // Prints an empty string
991+ ```
992+
993+ ``` cjs
960994const url = require (' url' );
995+
961996console .log (url .domainToUnicode (' xn--espaol-zwa.com' ));
962997// Prints español.com
963998console .log (url .domainToUnicode (' xn--fiq228c.com' ));
@@ -1079,7 +1114,26 @@ added: v15.7.0
10791114This utility function converts a URL object into an ordinary options object as
10801115expected by the [ ` http.request() ` ] [ ] and [ ` https.request() ` ] [ ] APIs.
10811116
1082- ``` js
1117+ ``` mjs
1118+ import { urlToHttpOptions } from ' url' ;
1119+ const myURL = new URL (' https://a:b@測試?abc#foo' );
1120+
1121+ console .log (urlToHttpOptions (myUrl));
1122+ /**
1123+ {
1124+ protocol: 'https:',
1125+ hostname: 'xn--g6w251d',
1126+ hash: '#foo',
1127+ search: '?abc',
1128+ pathname: '/',
1129+ path: '/?abc',
1130+ href: 'https://a:b@xn--g6w251d/?abc#foo',
1131+ auth: 'a:b'
1132+ }
1133+ */
1134+ ```
1135+
1136+ ``` cjs
10831137const { urlToHttpOptions } = require (' url' );
10841138const myURL = new URL (' https://a:b@測試?abc#foo' );
10851139
@@ -1124,8 +1178,8 @@ changes:
11241178
11251179> Stability: 3 - Legacy: Use the WHATWG URL API instead.
11261180
1127- The legacy ` urlObject ` (` require('url').Url ` ) is created and returned by the
1128- ` url.parse() ` function.
1181+ The legacy ` urlObject ` (` require('url').Url ` or ` import { Url } from 'url' ` ) is
1182+ created and returned by the ` url.parse() ` function.
11291183
11301184#### ` urlObject.auth `
11311185
@@ -1499,7 +1553,6 @@ console.log(myURL.origin);
14991553[ `https.request()` ] : https.md#https_https_request_options_callback
15001554[ `new URL()` ] : #url_new_url_input_base
15011555[ `querystring` ] : querystring.md
1502- [ `require('url').format()` ] : #url_url_format_url_options
15031556[ `url.domainToASCII()` ] : #url_url_domaintoascii_domain
15041557[ `url.domainToUnicode()` ] : #url_url_domaintounicode_domain
15051558[ `url.format()` ] : #url_url_format_urlobject
0 commit comments