教师:王晓玲QQ:4260924邮箱:4260924@qq.comNo de.js开发主要内容URL的结构图WHATWGAPI的常用属性和方法URLSearchParams的使用Node中 的URL一个URL字符串是一个结构化的字符串,它包含多个有意义的组成部分。当被解析时,会返回一个URL对象,它包含每个 组成部分作为属性。url模块提供了两套API来处理URLs:一个是Node.js遗留的特有的API,另一个则是通常使用在web浏 览器中实现了WHATWGURLStandard的API.请注意:虽然Node.js遗留的特有的API并没有被弃用,但是保 留的目的是用于向后兼容已有应用程序。因此新的应用程序请使用WHATWGAPI。WHATWG与Node.js遗留的特有的API的 比较如下。网址''http://user:pass@sub.host.com:8080/p/a/t/h?query=string#h ash''上方是由遗留的url.parse()返回的对象属性。网址下方的则是由WHATWGURL对象的属性。URL结构图WH ATWGURL的origin属性包括protocol和host,但不包含username、password。URL解析 1、利用WHATWGAPI解析一个URL字符串:const{URL}=require(''url'');const myURL=newURL(''https://user:pass@sub.host.com:8080/p/a/t/h?query =string#hash'');2、使用遗留的API解析URL字符串:consturl=require(''ur l'');constmyURL=url.parse(''https://user:pass@sub.host.com:8080 /p/a/t/h?query=string#hash'');WHATWGURLAPIConstructor:newU RL(input[,base])input要解析的输入URLbase| 如果“input”是相对URL,则为要解析的基本URL。通过将input解析到base上创建一个新的URL对象。const{ URL}=require(''url'');constmyURL=newURL(''/foo'',''https://e xample.org/'');//https://example.org/foo如果input或base是无效URLs,将 会抛出TypeError。例如:const{URL}=require(''url'');constmyURL=ne wURL({toString:()=>''https://example.org/''});//https://ex ample.org/存在于input主机名中的Unicode字符将被使用Punycode算法自动转换为ASCII。const {URL}=require(''url'');constmyURL=newURL(''https://你好你好''); //https://xn--6qqa088eba/url.hash属性获取及设置URL的分段(hash )部分。const{URL}=require(''url'');constmyURL=newURL(''https ://example.org/foo#bar'');console.log(myURL.hash);//输出#barm yURL.hash=''baz'';console.log(myURL.href);//输出https://examp le.org/foo#baz包含在赋给hash属性的值中的无效URL字符是[百分比编码][]。注意选择哪些字符进行百分比编码可能 与[url.parse()][]和[url.format()][]方法产生的不同。url.host属性获取 及设置URL的主机(host)部分。const{URL}=require(''url'');constmyURL= newURL(''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)部分。ur l.host和url.hostname之间的区别是url.hostname不包含端口。const{URL}=requ ire(''url'');constmyURL=newURL(''https://example.org:81/foo''); console.log(myURL.hostname);//输出example.orgmyURL.hostname= ''example.com:82'';console.log(myURL.href);//输出https://examp le.com:81/foo如果给hostname属性设置的值是无效值,那么该值将被忽略。url.href属性g>获取及设置序列化的URL。const{URL}=require(''url'');constmyURL=ne wURL(''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()。将此属性的值设置为新值等同于newURL(value)使用创建新的URL对象。U RL对象的每个属性都将被修改。如果给href属性设置的值是无效URL,将会抛出TypeError。URL的其它属性序号 属性&描述1url.origin获取只读序列化的URLorigin部分2url.password获取及设置URL的 密码(password)部分3url.pathname获取及设置URL的路径(path)部分4url.port获取及设 置URL的端口(port)部分端口值可以被设置为数字或包含数字的字符串,数字范围0~65535(包括)。5url.protoc ol获取及设置URL的协议(protocol)部分。6url.search获取及设置URL的序列化查询(query)部分部 分。7url.username获取及设置URL的用户名(username)部分。url.toJSON()方法返回:tring>在URL对象上调用toJSON()方法将返回序列化的URL。返回值与url.href和url.toString()的 相同。当URL对象使用JSON.stringify()序列化时将自动调用该方法。const{URL}=require (''url'');constmyURLs=[newURL(''https://www.example.com''), newURL(''https://test.example.org'')];console.log(JSON.stringif y(myURLs));//输出["https://www.example.com/","https://test.exa mple.org/"]url.format方法url.format(URL[,options])参数:URL 一个WHATWGURL对象options |
|