开发手册 欢迎您!
软件开发者资料库

URL | Node.js

Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型,使其轻量又高效。Node.js 的包管理器 npm,是全球最大的开源库生态系统。

Node.js v8.x 中文文档


url#

稳定性: 2 - 稳定的

url 模块提供了一些实用函数,用于 URL 处理与解析。可以通过以下方式使用:

const url = require('url');

URL 字符串与 URL 对象#

一个 URL 字符串是一个结构化的字符串,它包含多个有意义的组成部分。当被解析时,会返回一个 URL 对象,它包含每个组成部分作为属性。

url模块提供了两套API来处理URLs:一个是Node.js遗留的特有的API,另一个则是通常使用在web浏览器中实现了WHATWG URL Standard的API.

请注意: 虽然Node.js遗留的特有的API并没有被弃用,但是保留的目的是用于向后兼容已有应用程序。因此新的应用程序请使用WHATWG API。

WHATWG与Node.js遗留的特有的API的比较如下。网址'http://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash'上方是由遗留的url.parse()返回的对象属性。网址下方的则是由WHATWG URL对象的属性。

WHATWG URL的origin属性包括protocolhost,但不包含usernamepassword.

┌─────────────────────────────────────────────────────────────────────────────────────────────┐│                                            href                                             │├──────────┬──┬─────────────────────┬─────────────────────┬───────────────────────────┬───────┤│ protocol │  │        auth         │        host         │           path            │ hash  ││          │  │                     ├──────────────┬──────┼──────────┬────────────────┤       ││          │  │                     │   hostname   │ port │ pathname │     search     │       ││          │  │                     │              │      │          ├─┬──────────────┤       ││          │  │                     │              │      │          │ │    query     │       │"  https:   //    user   :   pass   @ sub.host.com : 8080   /p/a/t/h  ?  query=string   #hash "│          │  │          │          │   hostname   │ port │          │                │       ││          │  │          │          ├──────────────┴──────┤          │                │       ││ protocol │  │ username │ password │        host         │          │                │       │├──────────┴──┼──────────┴──────────┼─────────────────────┤          │                │       ││   origin    │                     │       origin        │ pathname │     search     │ hash  │├─────────────┴─────────────────────┴─────────────────────┴──────────┴────────────────┴───────┤│                                            href                                             │└─────────────────────────────────────────────────────────────────────────────────────────────┘(请忽略字符串中的空格,它们只是为了格式化)

利用WHATWG API解析一个URL字符串:

const { URL } = require('url');const myURL =  new URL('https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash');

在浏览器中,WHATWG URL在全局总是可用的,而在Node.js中,任何情况下打开或使用一个链接都必须事先引用'url'模块:require('url').URL

通过Node.js提供的API解析一个URL:

const url = require('url');const myURL =  url.parse('https://user:pass@sub.host.com:8080/p/a/t/h?query=string#hash');

The WHATWG URL API#

Class: URL#

浏览器兼容的 URL 类,根据 WHATWG URL 标准实现。[解析URL的示例][]可以在标准本身里边找到。

注意: 根据浏览器的约定,URL 对象的所有属性都是在类的原型上实现为getter和setter,而不是作为对象本身的数据属性。因此,与[遗留的urlObjects][]不同,在 URL 对象的任何属性(例如 delete myURL.protocoldelete myURL.pathname等)上使用 delete 关键字没有任何效果,但仍返回 true

Constructor: new URL(input[, base])#

  • input 要解析的输入URL
  • base | 如果“input”是相对URL,则为要解析的基本URL。

通过将input解析到base上创建一个新的URL对象。如果base是一个字符串,则解析方法与new URL(base)相同。

const { URL } = require('url');const myURL = new URL('/foo', 'https://example.org/');  // https://example.org/foo

如果inputbase是无效URLs,将会抛出TypeError。请注意给定值将被强制转换为字符串。例如:

const { URL } = require('url');const myURL = new URL({ toString: () => 'https://example.org/' });  // https://example.org/

存在于input主机名中的Unicode字符将被使用Punycode算法自动转换为ASCII。

const { URL } = require('url');const myURL = new URL('https://你好你好');  // https://xn--6qqa088eba/

Note: This feature is only available if the node executable was compiledwith ICU enabled. If not, the domain names are passed through unchanged.

url.hash#

获取及设置URL的分段(hash)部分。

const { URL } = require('url');const myURL = new URL('https://example.org/foo#bar');console.log(myURL.hash);  // 输出 #barmyURL.hash = 'baz';console.log(myURL.href);  // 输出 https://example.org/foo#baz

包含在赋给hash属性的值中的无效URL字符是[百分比编码][]。请注意选择哪些字符进行百分比编码可能与[url.parse()][]和[url.format()][]方法产生的不同。

url.host#

获取及设置URL的主机(host)部分。

const { URL } = require('url');const myURL = new URL('https://example.org:81/foo');console.log(myURL.host);  // 输出 example.org:81myURL.host = 'example.com:82';console.log(myURL.href);  // 输出 https://example.com:82/foo

如果给host属性设置的值是无效值,那么该值将被忽略。

url.hostname#

获取及设置URL的主机名(hostname)部分。 url.hosturl.hostname之间的区别是url.hostname 包含端口。

const { URL } = require('url');const myURL = new URL('https://example.org:81/foo');console.log(myURL.hostname);  // 输出 example.orgmyURL.hostname = 'example.com:82';console.log(myURL.href);  // 输出 https://example.com:81/foo

如果给hostname属性设置的值是无效值,那么该值将被忽略。

url.href#

获取及设置序列化的URL。

const { URL } = require('url');const myURL = new URL('https://example.org/foo');console.log(myURL.href);  // 输出 https://example.org/foomyURL.href = 'https://example.com/bar';console.log(myURL.href);  // 输出 https://example.com/bar

获取href属性的值等同于调用url.toString()

将此属性的值设置为新值等同于new URL(value)使用创建新的URL对象。URL对象的每个属性都将被修改。

如果给href属性设置的值是无效URL,将会抛出TypeError

url.origin#

获取只读序列化的URL origin部分。

const { URL } = require('url');const myURL = new URL('https://example.org/foo/bar?baz');console.log(myURL.origin);  // 输出 https://example.org
const { URL } = require('url');const idnURL = new URL('https://你好你好');console.log(idnURL.origin);  // 输出 https://xn--6qqa088ebaconsole.log(idnURL.hostname);  // 输出 xn--6qqa088eba

url.password#

获取及设置URL的密码(password)部分。

const { URL } = require('url');const myURL = new URL('https://abc:xyz@example.com');console.log(myURL.password);  // 输出 xyzmyURL.password = '123';console.log(myURL.href);  // 输出 https://abc:123@example.com

包含在赋给password属性的值中的无效URL字符是[百分比编码][]。请注意选择哪些字符进行百分比编码可能与url.parse()url.format()方法产生的不同。

url.pathname#

获取及设置URL的路径(path)部分。

const { URL } = require('url');const myURL = new URL('https://example.org/abc/xyz?123');console.log(myURL.pathname);  // 输出 /abc/xyzmyURL.pathname = '/abcdef';console.log(myURL.href);  // 输出 https://example.org/abcdef?123

包含在赋给pathname属性的值中的无效URL字符是[百分比编码][]。请注意选择哪些字符进行百分比编码可能与url.parse()url.format()方法产生的不同。

url.port#

获取及设置URL的端口(port)部分。

const { URL } = require('url');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/myURL.port = 1234;console.log(myURL.port);  // 输出 1234console.log(myURL.href);  // 输出 https://example.org:1234/// 完全无效的端口字符串将被忽略myURL.port = 'abcd';console.log(myURL.port);  // 输出 1234// 开头的数字将会被当做端口数myURL.port = '5678abcd';console.log(myURL.port);  // 输出 5678// 非整形数字将会被截取部分myURL.port = 1234.5678;console.log(myURL.port);  // 输出 1234// 超出范围的数字将被忽略myURL.port = 1e10;console.log(myURL.port);  // 输出 1234

端口值可以被设置为数字或包含数字的字符串,数字范围0~65535(包括)。为给定protocolURL对象设置端口值将会导致port值变成空字符('')。

如果给port属性设置的值是无效字符串,但如果字符串以数字开头,那么开头部位的数字将会被赋值给port。否则,包括如果数字超出上述要求的数字,将被忽略。

url.protocol#

获取及设置URL的协议(protocol)部分。

const { URL } = require('url');const myURL = new URL('https://example.org');console.log(myURL.protocol);  // 输出 https:myURL.protocol = 'ftp';console.log(myURL.href);  // 输出 ftp://example.org/

如果给protocol属性设置的值是无效值,那么该值将被忽略。

url.search#

获取及设置URL的序列化查询(query)部分部分。

const { URL } = require('url');const myURL = new URL('https://example.org/abc?123');console.log(myURL.search);  // 输出 ?123myURL.search = 'abc=xyz';console.log(myURL.href);  // 输出 https://example.org/abc?abc=xyz

任何出现在赋给search属性值中的无效URL字符将被[百分比编码][]。请注意选择哪些字符进行百分比编码可能与url.parse()url.format()方法产生的不同。

url.searchParams#

获取表示URL查询参数的URLSearchParams对象。该属性是只读的;使用url.search设置来替换URL的整个查询参数。请打开URLSearchParams文档来查看更多细节。

url.username#

获取及设置URL的用户名(username)部分。

const { URL } = require('url');const myURL = new URL('https://abc:xyz@example.com');console.log(myURL.username);  // 输出 abcmyURL.username = '123';console.log(myURL.href);  // 输出 https://123:xyz@example.com/

任何出现在赋给username属性值中的无效URL字符将被[百分比编码][]。请注意选择哪些字符进行百分比编码可能与url.parse()url.format()方法产生的不同。

url.toString()#

  • 返回:

URL对象上调用toString()方法将返回序列化的URL。返回值与url.hrefurl.toJSON()的相同。

由于需要符合标准,此方法不允许用户自定义URL的序列化过程。 如果需要更大灵活性,require('url').format()可能更合适。

url.toJSON()#

  • 返回:

URL对象上调用toJSON()方法将返回序列化的URL。返回值与url.hrefurl.toString()的相同。

URL对象使用JSON.stringify()序列化时将自动调用该方法。

const { URL } = require('url');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/"]

Class: URLSearchParams#

URLSearchParamsAPI接口提供对URLquery部分的读写权限。URLSearchParams类也能够与以下四个构造函数中的任意一个单独使用。

WHATWG URLSearchParams接口和querystring模块有相似的目的,但是querystring模块的目的更加通用,因为它可以定制分隔符(=)。但另一方面,这个API是专门为URL查询字符串而设计的。

const { URL, URLSearchParams } = require('url');const myURL = new URL('https://example.org/?abc=123');console.log(myURL.searchParams.get('abc'));// 输出 123myURL.searchParams.append('abc', 'xyz');console.log(myURL.href);// 输出 https://example.org/?abc=123&abc=xyzmyURL.searchParams.delete('abc');myURL.searchParams.set('a', 'b');console.log(myURL.href);// 输出 https://example.org/?a=bconst newSearchParams = new URLSearchParams(myURL.searchParams);// 上面的代码等同于// const newSearchParams = new URLSearchParams(myURL.search);newSearchParams.append('a', 'c');console.log(myURL.href);// 输出 https://example.org/?a=bconsole.log(newSearchParams.toString());// 输出 a=b&a=c// newSearchParams.toString() 被隐式调用myURL.search = newSearchParams;console.log(myURL.href);// 输出 https://example.org/?a=b&a=cnewSearchParams.delete('a');console.log(myURL.href);// 输出 https://example.org/?a=b&a=c

Constructor: new URLSearchParams()#

实例化一个新的空的URLSearchParams对象。

Constructor: new URLSearchParams(string)#

  • string 一个查询字符串

string解析成一个查询字符串, 并且使用它来实例化一个新的URLSearchParams对象. 如果string'?'打头,则'?'将会被忽略.

const { URLSearchParams } = require('url');let params;params = new URLSearchParams('user=abc&query=xyz');console.log(params.get('user'));  // 输出 'abc'console.log(params.toString());  // 输出 'user=abc&query=xyz'params = new URLSearchParams('?user=abc&query=xyz');console.log(params.toString());  // 输出 'user=abc&query=xyz'

Constructor: new URLSearchParams(obj)#