Skip to content

Commit 11e428d

Browse files
watildejasnell
authored andcommitted
doc: add require modules in url.md
To make the example codes in URL doc work without additional codes, it should have more lines to require modules. PR-URL: #13365 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
1 parent 2d25e09 commit 11e428d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

doc/api/url.md

+19
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Creates a new `URL` object by parsing the `input` relative to the `base`. If
9292
`base` is passed as a string, it will be parsed equivalent to `new URL(base)`.
9393

9494
```js
95+
const { URL } = require('url');
9596
const myURL = new URL('/foo', 'https://example.org/');
9697
// https://example.org/foo
9798
```
@@ -101,6 +102,7 @@ that an effort will be made to coerce the given values into strings. For
101102
instance:
102103

103104
```js
105+
const { URL } = require('url');
104106
const myURL = new URL({ toString: () => 'https://example.org/' });
105107
// https://example.org/
106108
```
@@ -109,6 +111,7 @@ Unicode characters appearing within the hostname of `input` will be
109111
automatically converted to ASCII using the [Punycode][] algorithm.
110112

111113
```js
114+
const { URL } = require('url');
112115
const myURL = new URL('https://你好你好');
113116
// https://xn--6qqa088eba/
114117
```
@@ -122,6 +125,7 @@ Additional [examples of parsed URLs][] may be found in the WHATWG URL Standard.
122125
Gets and sets the fragment portion of the URL.
123126

124127
```js
128+
const { URL } = require('url');
125129
const myURL = new URL('https://example.org/foo#bar');
126130
console.log(myURL.hash);
127131
// Prints #bar
@@ -143,6 +147,7 @@ percent-encode may vary somewhat from what the [`url.parse()`][] and
143147
Gets and sets the host portion of the URL.
144148

145149
```js
150+
const { URL } = require('url');
146151
const myURL = new URL('https://example.org:81/foo');
147152
console.log(myURL.host);
148153
// Prints example.org:81
@@ -163,6 +168,7 @@ Gets and sets the hostname portion of the URL. The key difference between
163168
port.
164169

165170
```js
171+
const { URL } = require('url');
166172
const myURL = new URL('https://example.org:81/foo');
167173
console.log(myURL.hostname);
168174
// Prints example.org
@@ -181,6 +187,7 @@ Invalid hostname values assigned to the `hostname` property are ignored.
181187
Gets and sets the serialized URL.
182188

183189
```js
190+
const { URL } = require('url');
184191
const myURL = new URL('https://example.org/foo');
185192
console.log(myURL.href);
186193
// Prints https://example.org/foo
@@ -209,12 +216,14 @@ may be contained within the hostname will be encoded as-is without [Punycode][]
209216
encoding.
210217

211218
```js
219+
const { URL } = require('url');
212220
const myURL = new URL('https://example.org/foo/bar?baz');
213221
console.log(myURL.origin);
214222
// Prints https://example.org
215223
```
216224

217225
```js
226+
const { URL } = require('url');
218227
const idnURL = new URL('https://你好你好');
219228
console.log(idnURL.origin);
220229
// Prints https://你好你好
@@ -230,6 +239,7 @@ console.log(idnURL.hostname);
230239
Gets and sets the password portion of the URL.
231240

232241
```js
242+
const { URL } = require('url');
233243
const myURL = new URL('https://abc:[email protected]');
234244
console.log(myURL.password);
235245
// Prints xyz
@@ -251,6 +261,7 @@ percent-encode may vary somewhat from what the [`url.parse()`][] and
251261
Gets and sets the path portion of the URL.
252262

253263
```js
264+
const { URL } = require('url');
254265
const myURL = new URL('https://example.org/abc/xyz?123');
255266
console.log(myURL.pathname);
256267
// Prints /abc/xyz
@@ -272,6 +283,7 @@ to percent-encode may vary somewhat from what the [`url.parse()`][] and
272283
Gets and sets the port portion of the URL.
273284

274285
```js
286+
const { URL } = require('url');
275287
const myURL = new URL('https://example.org:8888');
276288
console.log(myURL.port);
277289
// Prints 8888
@@ -327,6 +339,7 @@ lies outside the range denoted above, it is ignored.
327339
Gets and sets the protocol portion of the URL.
328340

329341
```js
342+
const { URL } = require('url');
330343
const myURL = new URL('https://example.org');
331344
console.log(myURL.protocol);
332345
// Prints https:
@@ -345,6 +358,7 @@ Invalid URL protocol values assigned to the `protocol` property are ignored.
345358
Gets and sets the serialized query portion of the URL.
346359

347360
```js
361+
const { URL } = require('url');
348362
const myURL = new URL('https://example.org/abc?123');
349363
console.log(myURL.search);
350364
// Prints ?123
@@ -375,6 +389,7 @@ documentation for details.
375389
Gets and sets the username portion of the URL.
376390

377391
```js
392+
const { URL } = require('url');
378393
const myURL = new URL('https://abc:[email protected]');
379394
console.log(myURL.username);
380395
// Prints abc
@@ -412,6 +427,7 @@ This method is automatically called when an `URL` object is serialized
412427
with [`JSON.stringify()`][].
413428

414429
```js
430+
const { URL } = require('url');
415431
const myURLs = [
416432
new URL('https://www.example.com'),
417433
new URL('https://test.example.org')
@@ -696,6 +712,7 @@ with the same name is preserved.
696712
This method can be used, in particular, to increase cache hits.
697713

698714
```js
715+
const { URLSearchParams } = require('url');
699716
const params = new URLSearchParams('query[]=abc&type=search&query[]=123');
700717
params.sort();
701718
console.log(params.toString());
@@ -810,6 +827,7 @@ of the output.
810827
For example:
811828

812829
```js
830+
const { URL } = require('url');
813831
const myURL = new URL('https://a:b@你好你好?abc#foo');
814832

815833
console.log(myURL.href);
@@ -1043,6 +1061,7 @@ manner similar to that of a Web browser resolving an anchor tag HREF.
10431061
For example:
10441062

10451063
```js
1064+
const url = require('url');
10461065
url.resolve('/one/two/three', 'four'); // '/one/two/four'
10471066
url.resolve('http://example.com/', '/one'); // 'http://example.com/one'
10481067
url.resolve('http://example.com/one', '/two'); // 'http://example.com/two'

0 commit comments

Comments
 (0)