9
9
The ` url ` module provides utilities for URL resolution and parsing. It can be
10
10
accessed using:
11
11
12
- ``` js
12
+ ``` mjs
13
+ import url from ' url' ;
14
+ ```
15
+
16
+ ``` cjs
13
17
const url = require (' url' );
14
18
```
15
19
@@ -61,7 +65,13 @@ const myURL =
61
65
62
66
Parsing the URL string using the Legacy API:
63
67
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
65
75
const url = require (' url' );
66
76
const myURL =
67
77
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/');
135
145
The URL constructor is accessible as a property on the global object.
136
146
It can also be imported from the built-in url module:
137
147
138
- ``` js
148
+ ``` mjs
149
+ import { URL } from ' url' ;
150
+ console .log (URL === globalThis .URL ); // Prints 'true'.
151
+ ```
152
+
153
+ ``` cjs
139
154
console .log (URL === require (' url' ).URL ); // Prints 'true'.
140
155
```
141
156
@@ -573,10 +588,6 @@ and [`url.format()`][] methods would produce.
573
588
The ` toString() ` method on the ` URL ` object returns the serialized URL. The
574
589
value returned is equivalent to that of [ ` url.href ` ] [ ] and [ ` url.toJSON() ` ] [ ] .
575
590
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
-
580
591
#### ` url.toJSON() `
581
592
582
593
* Returns: {string}
@@ -931,8 +942,20 @@ invalid domain, the empty string is returned.
931
942
932
943
It performs the inverse operation to [ ` url.domainToUnicode() ` ] [ ] .
933
944
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
935
957
const url = require (' url' );
958
+
936
959
console .log (url .domainToASCII (' español.com' ));
937
960
// Prints xn--espaol-zwa.com
938
961
console .log (url .domainToASCII (' 中文.com' ));
@@ -956,8 +979,20 @@ domain, the empty string is returned.
956
979
957
980
It performs the inverse operation to [ ` url.domainToASCII() ` ] [ ] .
958
981
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
960
994
const url = require (' url' );
995
+
961
996
console .log (url .domainToUnicode (' xn--espaol-zwa.com' ));
962
997
// Prints español.com
963
998
console .log (url .domainToUnicode (' xn--fiq228c.com' ));
@@ -1079,7 +1114,26 @@ added: v15.7.0
1079
1114
This utility function converts a URL object into an ordinary options object as
1080
1115
expected by the [ ` http.request() ` ] [ ] and [ ` https.request() ` ] [ ] APIs.
1081
1116
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
1083
1137
const { urlToHttpOptions } = require (' url' );
1084
1138
const myURL = new URL (' https://a:b@測試?abc#foo' );
1085
1139
@@ -1124,8 +1178,8 @@ changes:
1124
1178
1125
1179
> Stability: 3 - Legacy: Use the WHATWG URL API instead.
1126
1180
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.
1129
1183
1130
1184
#### ` urlObject.auth `
1131
1185
@@ -1499,7 +1553,6 @@ console.log(myURL.origin);
1499
1553
[ `https.request()` ] : https.md#https_https_request_options_callback
1500
1554
[ `new URL()` ] : #url_new_url_input_base
1501
1555
[ `querystring` ] : querystring.md
1502
- [ `require('url').format()` ] : #url_url_format_url_options
1503
1556
[ `url.domainToASCII()` ] : #url_url_domaintoascii_domain
1504
1557
[ `url.domainToUnicode()` ] : #url_url_domaintounicode_domain
1505
1558
[ `url.format()` ] : #url_url_format_urlobject
0 commit comments