URL 模块
基础
URL 组成
┌────────────────────────────────────────────────────────────────────────────────────────────────┐
│ href │
├──────────┬──┬─────────────────────┬────────────────────────┬───────────────────────────┬───────┤
│ protocol │ │ auth │ host │ path │ hash │
│ │ │ ├─────────────────┬──────┼──────────┬────────────────┤ │
│ │ │ │ hostname │ port │ pathname │ search │ │
│ │ │ │ │ │ ├─┬──────────────┤ │
│ │ │ │ │ │ │ │ query │ │
" https: // user : pass @ sub.example.com : 8080 /p/a/t/h ? query=string #hash "
│ │ │ │ │ hostname │ port │ │ │ │
│ │ │ │ ├─────────────────┴──────┤ │ │ │
│ protocol │ │ username │ password │ host │ │ │ │
├──────────┴──┼──────────┴──────────┼────────────────────────┤ │ │ │
│ origin │ │ origin │ pathname │ search │ hash │
├─────────────┴─────────────────────┴────────────────────────┴──────────┴────────────────┴───────┤
│ href │
└────────────────────────────────────────────────────────────────────────────────────────────────┘
(All spaces in the "" line should be ignored. They are purely for formatting.)
URL
属性
// https://abc:xyz@example.org:8080/foo?123#bar
url.href; // https://abc:xyz@example.org:8080/foo?123#bar
url.protocol; // https:
url.origin; // https://example.org:8080/
url.username; // abc
url.password; // xyz
url.host; // example.org:8080
url.hostname; // example.com
url.port; // 8080
url.pathname; // /foo
url.search; // ?123
url.searchParams; // ?123
url.hash; // # bar
方法
创建 URL
// new URL(input[, base])
const myURL = new URL("/foo", "https://example.org/"); // https://example.org/foo
转换
// url.toString(), 等效于 url.href
// url.toJSON(), 等效于 url.href
// URL.canParse(input[, base])
// url 是否能够被解析
const isValid = URL.canParse("/foo", "https://example.org/"); // true
const isNotValid = URL.canParse("/foo"); // false
URLSearchParams
属性
属性
urlSearchParams.size; // 参数数量
方法
创建
// new URLSearchParams(string)
const params = new URLSearchParams("user=abc&query=xyz");
// new URLSearchParams(obj)
const params = new URLSearchParams({
user: "abc",
query: ["first", "second"],
});
// new URLSearchParams(iterable)
const params = new URLSearchParams([
["user", "abc"],
["query", "first"],
["query", "second"],
]);
crud
// urlSearchParams.has(name[, value])
// urlSearchParams.set(name, value)
// urlSearchParams.append(name, value)
// urlSearchParams.get(name)
// urlSearchParams.getAll(name)
// urlSearchParams.delete(name[, value])
排序
// urlSearchParams.sort()
迭代
// urlSearchParams.entries()
// urlSearchParams.keys()
// urlSearchParams.values()
// urlSearchParams.forEach(fn[, thisArg])
转换
// urlSearchParams.toString()
API
转换
// url.domainToASCII(domain)
// 转换为 ASCII 码
console.log(url.domainToASCII("中文.com")); // xn--fiq228c.com
// url.domainToUnicode(domain)
// 转换为 Unicode 码
console.log(url.domainToUnicode("xn--fiq228c.com")); // 中文.com
// url.fileURLToPath(url)
// fileURL 转换为 path, 跨平台支持, Unicode 编码
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.pathToFileURL(path)
// path 转换为 fileURL, 跨平台支持
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.format(URL[, options])
const myURL = new URL("https://a:b@測試?abc#foo");
console.log(
url.format(myURL, {
fragment: false, // 是否包括 hash, 默认 true
search: true, // 是否包括 search, 默认 true
unicode: true, // 使用使用 unicode 编码, 默认 false
auth: false, // 是否包括 auth, 默认 false
})
); // 'https://測試/?abc'
// url.urlToHttpOptions(url)
// 使用 ASCII 编码
const myURL = new URL("https://a:b@測試?abc#foo");
console.log(urlToHttpOptions(myURL));
/*
{
protocol: 'https:',
hostname: 'xn--g6w251d',
hash: '#foo',
search: '?abc',
pathname: '/',
path: '/?abc',
href: 'https://a:b@xn--g6w251d/?abc#foo',
auth: 'a:b'
}
*/