Url模块

一、Url组件

┌─────────────────────────────────────────────────────────────────────────────┐
│                                    href                                     │
├──────────┬┬───────────┬─────────────────┬───────────────────────────┬───────┤
│ protocol ││   auth    │      host       │           path            │ hash  │
│          ││           ├──────────┬──────┼──────────┬────────────────┤       │
│          ││           │ hostname │ port │ pathname │     search     │       │
│          ││           │          │      │          ├─┬──────────────┤       │
│          ││           │          │      │          │ │    query     │       │
"  http:   // user:pass @ host.com : 8080   /p/a/t/h  ?  query=string   #hash "
│          ││           │          │      │          │ │              │       │
└──────────┴┴───────────┴──────────┴──────┴──────────┴─┴──────────────┴───────┘
(all spaces in the "" line should be ignored -- they're purely for formatting)

二、Url对象

  • objURL=url.parse(urlStr,[parseQueryString],[slasheDenoteHost])
  • 将第二个参数设置为 true 则使用 querystring 模块来解析 URL 中的查询字符串部分,默认为 false
  • 将第三个参数设置为 true 来把诸如 //foo/bar 这样的URL解析为 { host: 'foo', pathname: '/bar' } 而不是 { pathname: '//foo/bar' }。 默认为 false
 属性 说明 
 href  完整的原始URL字符串
 protocol  请求协议,小写
 host  URL德尔完整主机部分,包括端口信息,小写
 auth  URL的身份验证信息部分
 hostname  主机的主机名部分
 port  主机的端口部分
 pathname  URL的路径部分
 search  URL的查询字符串部分,包括前导的问号
 path  完整路径,包括路径和搜索
 query  查询字符串中的参数部分或含有查询字符串参数和值的解析后对象,如果parseQueryString设置为true,则为后者
 hash  URL的散列部分,包括井号

三、解析URL组件到新的位置

  1、url.resolve(from,to)  //原始基础URL字符串  指定解析到的新位置

四、格式化URL对象为URL字符串

  1、url.format(urlObject);  //将URL对象格式化为URL字符串

All rights reserved please indicate the source if reprint---吓尿了的大肥鼠
原文地址:https://www.cnblogs.com/realsoul/p/5624893.html