diff --git a/doc/api/url.md b/doc/api/url.md index 2330755a6f1108..a96df0adb72f90 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -978,17 +978,18 @@ 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) +const url = require('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 -fileURLToPath('file://nas/foo.txt'); // Correct: \\nas\foo.txt (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 -fileURLToPath('file:///你好.txt'); // Correct: /你好.txt (POSIX) +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 -fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX) +new URL('file:///hello world').pathname; // Incorrect: /hello%20world +url.fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX) ``` ### `url.format(URL[, options])` @@ -1018,6 +1019,7 @@ any way. The `url.format(URL[, options])` method allows for basic customization of the output. ```js +const url = require('url'); const myURL = new URL('https://a:b@測試?abc#foo'); console.log(myURL.href); @@ -1042,16 +1044,17 @@ 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) +const url = require('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 -pathToFileURL('/foo#1'); // Correct: file:///foo%231 (POSIX) +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 -pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX) +new URL('/some/path%.c', 'file:'); // Incorrect: file:///some/path%.c +url.pathToFileURL('/some/path%.c'); // Correct: file:///some/path%25.c (POSIX) ``` ### `url.urlToHttpOptions(url)` @@ -1258,6 +1261,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',