Skip to content

Commit ac2c8e1

Browse files
committed
docs: update code examples for node:url module
There are many things called `url` in this page including `url` module, `URL` instances, etc. The original example was not clear where these methods come from.
1 parent 44ffdda commit ac2c8e1

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

doc/api/url.md

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,17 +1013,18 @@ This function ensures the correct decodings of percent-encoded characters as
10131013
well as ensuring a cross-platform valid absolute path string.
10141014

10151015
```js
1016-
new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
1017-
fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)
1016+
const url = require('url');
1017+
new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
1018+
url.fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)
10181019

1019-
new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt
1020-
fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows)
1020+
new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt
1021+
url.fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows)
10211022

1022-
new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
1023-
fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX)
1023+
new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
1024+
url.fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX)
10241025

1025-
new URL('file:///hello world').pathname; // Incorrect: /hello%20world
1026-
fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX)
1026+
new URL('file:///hello world').pathname; // Incorrect: /hello%20world
1027+
url.fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX)
10271028
```
10281029

10291030
### `url.format(URL[, options])`
@@ -1053,6 +1054,7 @@ any way. The `url.format(URL[, options])` method allows for basic customization
10531054
of the output.
10541055

10551056
```js
1057+
const url = require('url');
10561058
const myURL = new URL('https://a:b@測試?abc#foo');
10571059

10581060
console.log(myURL.href);
@@ -1077,16 +1079,17 @@ This function ensures that `path` is resolved absolutely, and that the URL
10771079
control characters are correctly encoded when converting into a File URL.
10781080

10791081
```js
1080-
new URL(__filename); // Incorrect: throws (POSIX)
1081-
new URL(__filename); // Incorrect: C:\... (Windows)
1082-
pathToFileURL(__filename); // Correct: file:///... (POSIX)
1083-
pathToFileURL(__filename); // Correct: file:///C:/... (Windows)
1082+
const url = require('url');
1083+
new URL(__filename); // Incorrect: throws (POSIX)
1084+
new URL(__filename); // Incorrect: C:\... (Windows)
1085+
url.pathToFileURL(__filename); // Correct: file:///... (POSIX)
1086+
url.pathToFileURL(__filename); // Correct: file:///C:/... (Windows)
10841087

1085-
new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1
1086-
pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX)
1088+
new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1
1089+
url.pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX)
10871090

1088-
new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c
1089-
pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX)
1091+
new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c
1092+
url.pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX)
10901093
```
10911094

10921095
### `url.urlToHttpOptions(url)`
@@ -1317,6 +1320,7 @@ The `url.format()` method returns a formatted URL string derived from
13171320
`urlObject`.
13181321

13191322
```js
1323+
const url = require('url');
13201324
url.format({
13211325
protocol: 'https',
13221326
hostname: 'example.com',

0 commit comments

Comments
 (0)