手机版

Node.js学习之地址解析模块统一资源定位器的使用详解

时间:2021-09-03 来源:互联网 编辑:宝哥软件园 浏览:

前言

本文主要给大家介绍了关于Node.js地址解析模块统一资源定位器使用的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

全球资源定位器(统一资源定位符)结构化/模块化/路径解析

结构化:url.parse(urlString[,parseQueryString[,slashesDenoteHost]])模块化:url.format(urlObject)路径解析:url.resolve(从,到)一个统一资源定位器字符串是一个结构化的字符串包含多个有意义的组件。在解析时,返回一个统一资源定位器对象包含每一个组件的属性。

官方手册上面的一张图是这样子的:

这张图解释了一个全球资源定位器(统一资源定位符)结构化成哪些部分,哪些部分又包含哪些部分

协议:请求协议

主机:网址主机名已全部转换成小写,包括端口信息

auth:URL中身份验证信息部分

主机名:主机的主机名部分,已转换成小写

端口:主机的端口号部分

路径名:网址的路径部分,位于主机名之后请求查询之前

搜索:网址的"查询字符串"部分,包括开头的问号。

路径:路径名和搜索连在一起。

查询:查询字符串中的参数部分(问号后面部分字符串),或者使用querystring.parse()解析后返回的对象。

hash:网址的"#" 后面部分(包括# 符号)

全球资源定位器(统一资源定位符)结构化

将一个全球资源定位器(统一资源定位符)地址结构化成为拥有上图属性的全球资源定位器(统一资源定位符)对象url。解析第二个和第三个参数默认为假的。

第二个参数决定询问属性值是字符串还是对象第三个参数如果为true,//后的第一个令牌文字字符串和下一个/之间的文字字符串将被解释为主机例子如下

const URL=必选(' URL ');var URL str=' http://localhost :8888/bb?name=大熊备忘录=hello world memo=helloC ';var URL obj=URL。解析(网址字符串);控制台。日志(URL obj);/*Url { protocol: 'http: ',斜杠: true,auth: null,host: 'localhost:888 ',port: '8888 ',hostname: 'localhost ',hash: null,search:name=大熊备忘录=hello world memo=helloC ',查询: ' name=大熊备忘录=hello world memo=helloC ',pathname: '/bb ',path: '/bb?name=大熊memo=hello world memo=helloC ',href : ' http://localhost :8888/bb?name=大熊备忘录=hellowordmemo=helloC ' } */第二个参数为真实的时

query: { name: 'bigbear ',memo: [ 'helloworld ',' helloC' ] },例子如下:

const URL=必选(' URL ');var URL str=' http://localhost :8888/bb?name=大熊备忘录=hello world memo=helloC ';控制台。日志(Url。parse(urlst,true))/* URl { protocol : ' http : ',slashes: true,auth: null,host: ' localhost:8888 ',port: '8888 ',hostname: 'localhost ',hash: null,search: '?name=big bear memo=hello world memo=hello c ',query: { name: 'bigbear ',memo: [ 'helloworld ',' helloC' ] },pathname: '/bb ',path: '/bb?name=大熊memo=hello world memo=helloC ',href : ' http://localhost :8888/bb?name=大熊备忘录=hellowordmemo=helloC ' } */第三个参数对比

例子如下:

const URL=必选(' URL ');var URL str='//foo/bar ';console.log(url.parse(urlstr,true,true))/*输出:Url { protocol: null,slashes: true,auth: null,host: 'foo ',port: null,hostname: 'foo ',hash: null,search: ' ',query: {},pathname: '/bar ',href 3: '/foo/bar ' } */const URl=require(' URl ')var URl str='//foo/控制台。日志(网址。解析(网址字符串))/*输出:Url { protocol: null,slashes: null,auth: null,host: null,port: null,hostname: null,hash: null,search: null,query: null,路径名: '/foo/bar ',路径: '/foo/bar ',href : '/foo/bar ' } */Url模块化

将一个全球资源定位器(统一资源定位符)对象转换成一个全球资源定位器(统一资源定位符)字符串,url对象中的属性为url.parse()产生的对象的属性。

url.parse()和url.format()互为逆操作。

例子如下:

const URL=必选(' URL ');var URL obj={ protocol : ' http : ',slashes: true,auth: null,host: 'localhost:8888 ',port: '8888 ',hostname: 'localhost ',hash: null,search:name=big bear memo=hello world memo=hello c ',query: { name: 'bigbear ',memo: [ 'helloworld ',' helloC' ] },pathname: '/bb ',path: '/bb?name=big bear memo=hellowordmemo=helloC ',} console。日志(网址。格式(Ulbobj))//输出:http://localhost:8888/bb?name=大熊备忘录=hello world备忘录=helloC路径解析:url.resolve(从,到)

url.resolve()方法解决了目标统一资源定位器相对于基本统一资源定位器的方式类似于网浏览器解决锚标记href。

官方手册例子:

网址。解析('/一/二/三','四');//'/一/二/四'网址。resolve(' http://示例。com/','/one ');//' http://示例。' com/one '网址。resolve(' http://示例。com/one ','/two ');//'http://example.com/two '总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对我们的支持。

版权声明:Node.js学习之地址解析模块统一资源定位器的使用详解是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。