mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
doc: 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. PR-URL: https://github.com/nodejs/node/pull/38645 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
a98d631905
commit
5553be3f4e
1 file changed
+65
-19
+65
-19
@@ -1012,18 +1012,37 @@ added: v10.12.0
|
||||
This function ensures the correct decodings of percent-encoded characters as
|
||||
well as ensuring a cross-platform valid absolute path string.
|
||||
|
||||
```js
|
||||
new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
|
||||
fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)
|
||||
```mjs
|
||||
import { fileURLToPath } from 'url';
|
||||
|
||||
new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt
|
||||
fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows)
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
|
||||
new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
|
||||
fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX)
|
||||
new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
|
||||
fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)
|
||||
|
||||
new URL('file:///hello world').pathname; // Incorrect: /hello%20world
|
||||
fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX)
|
||||
new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt
|
||||
fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows)
|
||||
|
||||
new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
|
||||
fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX)
|
||||
|
||||
new URL('file:///hello world').pathname; // Incorrect: /hello%20world
|
||||
fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX)
|
||||
```
|
||||
|
||||
```cjs
|
||||
const { fileURLToPath } = require('url');
|
||||
new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/
|
||||
fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows)
|
||||
|
||||
new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt
|
||||
fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows)
|
||||
|
||||
new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt
|
||||
fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX)
|
||||
|
||||
new URL('file:///hello world').pathname; // Incorrect: /hello%20world
|
||||
fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX)
|
||||
```
|
||||
|
||||
### `url.format(URL[, options])`
|
||||
@@ -1052,7 +1071,22 @@ string serializations of the URL. These are not, however, customizable in
|
||||
any way. The `url.format(URL[, options])` method allows for basic customization
|
||||
of the output.
|
||||
|
||||
```js
|
||||
```mjs
|
||||
import url from 'url';
|
||||
const myURL = new URL('https://a:b@測試?abc#foo');
|
||||
|
||||
console.log(myURL.href);
|
||||
// Prints https://a:b@xn--g6w251d/?abc#foo
|
||||
|
||||
console.log(myURL.toString());
|
||||
// Prints https://a:b@xn--g6w251d/?abc#foo
|
||||
|
||||
console.log(url.format(myURL, { fragment: false, unicode: true, auth: false }));
|
||||
// Prints 'https://測試/?abc'
|
||||
```
|
||||
|
||||
```cjs
|
||||
const url = require('url');
|
||||
const myURL = new URL('https://a:b@測試?abc#foo');
|
||||
|
||||
console.log(myURL.href);
|
||||
@@ -1076,17 +1110,28 @@ added: v10.12.0
|
||||
This function ensures that `path` is resolved absolutely, and that the URL
|
||||
control characters are correctly encoded when converting into a File URL.
|
||||
|
||||
```js
|
||||
new URL(__filename); // Incorrect: throws (POSIX)
|
||||
new URL(__filename); // Incorrect: C:\... (Windows)
|
||||
pathToFileURL(__filename); // Correct: file:///... (POSIX)
|
||||
pathToFileURL(__filename); // Correct: file:///C:/... (Windows)
|
||||
```mjs
|
||||
import { pathToFileURL } from 'url';
|
||||
|
||||
new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1
|
||||
pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX)
|
||||
new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1
|
||||
pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX)
|
||||
|
||||
new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c
|
||||
pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX)
|
||||
new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c
|
||||
pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX)
|
||||
```
|
||||
|
||||
```cjs
|
||||
const { pathToFileURL } = require('url');
|
||||
new URL(__filename); // Incorrect: throws (POSIX)
|
||||
new URL(__filename); // Incorrect: C:\... (Windows)
|
||||
pathToFileURL(__filename); // Correct: file:///... (POSIX)
|
||||
pathToFileURL(__filename); // Correct: file:///C:/... (Windows)
|
||||
|
||||
new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1
|
||||
pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX)
|
||||
|
||||
new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c
|
||||
pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX)
|
||||
```
|
||||
|
||||
### `url.urlToHttpOptions(url)`
|
||||
@@ -1306,6 +1351,7 @@ The `url.format()` method returns a formatted URL string derived from
|
||||
`urlObject`.
|
||||
|
||||
```js
|
||||
const url = require('url');
|
||||
url.format({
|
||||
protocol: 'https',
|
||||
hostname: 'example.com',
|
||||
|
||||
Reference in new issue
Block a user