🌙 NodeJS之url模块学习笔记
🌙 1.URL模块简介
url 模块用于处理与解析 URL。 使用方法如下:
//方式一: 引入url模块
const url = require('url');
// 方式二:使用URL类,直接使用构造函数
// 解析URL字符串
url.parse('http://localhost:11014/#/order-agreement-manager?key=reward-distribution#a');
new URL('https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash');
2
3
4
5
6
7
// 结果如下:
{
"protocol": "https:", // 协议
"slashes": true, //
"auth": "user:pass",
"host": "sub.host.com:8080", // 域名
"port": "8080", // 端口号
"hostname": "sub.host.com",// 域名
"hash": "#hash", // 哈希
"search": "?query=string",
"query": "query=string",// 查询字符串(用querystring.parse解析)或url.searchParams解析
"pathname": "/p/a/t/h", // 路徑名
"path": "/p/a/t/h?query=string",
"href": "https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash"
}
2
3
4
5
6
7
8
9
10
11
12
13
14
URL 字符串是结构化的字符串,包含多个含义不同的组成部分。 解析字符串后返回的 URL 对象,每个属性对应字符串的各个组成部分。
🌙 2.构造方法new URL(input[, base])
inputstring (opens new window) 要解析的绝对或相对的 URL。如果input是相对路径,则需要base。 如果input是绝对路径,则忽略base。basestring (opens new window) | URL (opens new window) 如果input不是绝对路径,则为要解析的基本 URL。
通过将 input 相对于 base 进行解析,创建一个新的 URL 对象。 如果 base 是一个字符串,则解析方法与 new URL(base) 相同。
const myURL = new URL('/foo', 'https://example.org/');
// https://example.org/foo
2
🌙 3.实例属性
🌙 3.1url.hash获取与设置哈希
获取哈希
const myURL = new URL('https://example.org/foo#bar#zzz'); console.log(myURL.hash); // #bar#zzz1
2设置哈希
const myURL = new URL('https://example.org/foo#bar'); myURL.hash = 'baz'; console.log(myURL.href); // 打印 https://example.org/foo#baz1
2
3
4
🌙 3.2url.host获取及设置主机部分
const myURL = new URL('https://example.org:81/foo');
console.log(myURL.host); // example.org:81
myURL.host = 'example.com:82';
console.log(myURL.href); // https://example.com:82/foo
2
3
4
5
🌙 3.3 url.hostname获取及设置 URL 的主机名部分
url.host 和 url.hostname 之间的区别是 url.hostname 不包含端口。
const myURL = new URL('https://example.org:81/foo');
console.log(myURL.hostname);
// 打印 example.org
myURL.hostname = 'example.com:82';
console.log(myURL.href);
// 打印 https://example.com:81/foo
2
3
4
5
6
7
🌙 3.4 url.href获取及设置序列化的 URL
获取 href 属性的值等同于调用 url.toString() (opens new window)。
将此属性的值设置为新值等同于使用 new URL(value) (opens new window) 创建新的URL对象。 URL 对象的每个属性都将被修改。
const myURL = new URL('https://example.org/foo');
console.log(myURL.href);
// 打印 https://example.org/foo
myURL.href = 'https://example.com/bar';
console.log(myURL.href);
// 打印 https://example.com/bar
2
3
4
5
6
7
🌙 3.5 url.origin获取只读的序列化的 URL 的 origin
const myURL = new URL('https://example.org/foo/bar?baz');
console.log(myURL.origin);
// 打印 https://example.org
2
3
🌙 3.6 url.password获取及设置 URL 的密码部分
const myURL = new URL('https://abc:xyz@example.com');
console.log(myURL.password);
// 打印 xyz
myURL.password = '123';
console.log(myURL.href);
// 打印 https://abc:123@example.com
2
3
4
5
6
7
🌙 3.7 url.pathname获取及设置 URL 的路径部分
const myURL = new URL('https://example.org/abc/xyz?123');
console.log(myURL.pathname);
// 打印 /abc/xyz
myURL.pathname = '/abcdef';
console.log(myURL.href);
// 打印 https://example.org/abcdef?123
2
3
4
5
6
7
🌙 3.8url.port获取及设置 URL 的端口部分
端口值可以是数字或包含 0 到 65535(含)范围内的数字字符串。 将值设置为给定 protocol 的 URL 对象的默认端口将会导致 port 值变为空字符串('')。
端口值可以是空字符串,在这种情况下,端口取决于协议/规范:
| 协议 | 端口 |
|---|---|
| "ftp" | 21 |
| "file" | |
| "gopher" | 70 |
| "http" | 80 |
| "https" | 443 |
| "ws" | 80 |
| "wss" | 443 |
const myURL = new URL('https://example.org:8888');
console.log(myURL.port);
// 打印 8888
// 默认端口将自动转换为空字符。
// (HTTPS 协议默认端口是 443)
myURL.port = '443';
console.log(myURL.port);
// 打印空字符
console.log(myURL.href);
// 打印 https://example.org/
2
3
4
5
6
7
8
9
10
11
🌙 3.9 url.protocol
获取及设置 URL 的协议部分。
const myURL = new URL('https://example.org');
console.log(myURL.protocol);
// 打印 https:
myURL.protocol = 'ftp';
console.log(myURL.href);
// 打印 ftp://example.org/
2
3
4
5
6
7
分配给 protocol 属性的无效协议值将会被忽略。
🌙 3.10 url.search获取及设置 URL 的序列化查询部分
const myURL = new URL('https://example.org/abc?123');
console.log(myURL.search);
// 打印 ?123
myURL.search = 'abc=xyz';
console.log(myURL.href);
// 打印 https://example.org/abc?abc=xyz
2
3
4
5
6
7
🌙 3.11 url.searchParams获取表示 URL 查询参数
获取表示 URL 查询参数的 URLSearchParams (opens new window) 对象。 该属性是只读的。 使用 url.search (opens new window) 设置来替换 URL 的整个查询参数。 详见 URLSearchParams (opens new window)。
const myURL = new URL('https://example.org/abc?name=123&pwd=456');
// 1.获取
myURL.searchParams.get('name'); // 123
myURL.searchParams.get('pwd'); // 456
// 2.添加
myURL.searchParams.append('abc', 'xyz');
console.log(myURL.href);
// 打印 https://example.org/abc?name=123&pwd=456&abc=xyz
// 3.删除
myURL.searchParams.delete('abc');
// 4.更改(没有就添加)
myURL.searchParams.set('a', 'b');
console.log(myURL.href);
// 打印 https://example.org/?name=123&pwd=456&a=b
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
🌙 3.12 url.username获取及设置 URL 的用户名部分
const myURL = new URL('https://abc:xyz@example.com');
console.log(myURL.username);
// 打印 abc
myURL.username = '123';
console.log(myURL.href);
// 打印 https://123:xyz@example.com/
2
3
4
5
6
7
🌙 3.13 url.toString()
在 URL 对象上调用 toString() 方法将返回序列化的 URL。 返回值与 url.href (opens new window) 和 url.toJSON() (opens new window) 的相同。
🌙 3.14 url.toJSON()序列化 URL
在 URL 对象上调用 toJSON() 方法将返回序列化的 URL。 返回值与 url.href (opens new window) 和 url.toString() (opens new window) 的相同。
当 URL 对象使用 JSON.stringify() (opens new window) 序列化时将自动调用该方法。
const myURLs = [
new URL('https://www.example.com'),
new URL('https://test.example.org')
];
console.log(JSON.stringify(myURLs));
// 打印 ["https://www.example.com/","https://test.example.org/"]
2
3
4
5
6
🌙 4.小结
使用
const url = require('url') // 或者 const myURL = new URL('https://www.example.com')1
2
3方法
url.protocol url.hostname url.host url.port url.href url.pathname url.hash url.search url.searchParams ...1
2
3
4
5
6
7
8
9
10