diff --git a/doc/api/url.md b/doc/api/url.md index 3f95a9eaa286c2..8a3b8d457ec618 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -1012,7 +1012,22 @@ 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 +```mjs +import url from 'url'; +new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/ +url.fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows) + +new URL('file://nas/foo.txt').pathname; // Incorrect: /foo.txt +url.fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (Windows) + +new URL('file:///你好.txt').pathname; // Incorrect: /%E4%BD%A0%E5%A5%BD.txt +url.fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX) + +new URL('file:///hello world').pathname; // Incorrect: /hello%20world +url.fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX) +``` + +```cjs const url = require('url'); new URL('file:///C:/path/').pathname; // Incorrect: /C:/path/ url.fileURLToPath('file:///C:/path/'); // Correct: C:\path\ (Windows) @@ -1053,7 +1068,21 @@ 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'); @@ -1078,7 +1107,21 @@ 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 +```mjs +import url from 'url'; +new URL(__filename); // Incorrect: throws (POSIX) +new URL(__filename); // Incorrect: C:\... (Windows) +url.pathToFileURL(__filename); // Correct: file:///... (POSIX) +url.pathToFileURL(__filename); // Correct: file:///C:/... (Windows) + +new URL('/foo#1', 'file:'); // Incorrect: file:///foo#1 +url.pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX) + +new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c +url.pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX) +``` + +```cjs const url = require('url'); new URL(__filename); // Incorrect: throws (POSIX) new URL(__filename); // Incorrect: C:\... (Windows) @@ -1319,7 +1362,22 @@ changes: The `url.format()` method returns a formatted URL string derived from `urlObject`. -```js +```mjs +import url from 'url'; +url.format({ + protocol: 'https', + hostname: 'example.com', + pathname: '/some/path', + query: { + page: 1, + format: 'json' + } +}); + +// => 'https://example.com/some/path?page=1&format=json' +``` + +```cjs const url = require('url'); url.format({ protocol: 'https', @@ -1473,7 +1531,14 @@ changes: The `url.resolve()` method resolves a target URL relative to a base URL in a manner similar to that of a Web browser resolving an anchor tag HREF. -```js +```mjs +import url from 'url'; +url.resolve('/one/two/three', 'four'); // '/one/two/four' +url.resolve('http://example.com/', '/one'); // 'http://example.com/one' +url.resolve('http://example.com/one', '/two'); // 'http://example.com/two' +``` + +```cjs const url = require('url'); url.resolve('/one/two/three', 'four'); // '/one/two/four' url.resolve('http://example.com/', '/one'); // 'http://example.com/one'