NodeJS——URL网址解析

URL网址解析

[root@localhost ~]# node
> url
{ parse: [Function: urlParse],
  resolve: [Function: urlResolve],
  resolveObject: [Function: urlResolveObject],
  format: [Function: urlFormat],
  Url: [Function: Url] }

NodeJS 6.9.1版本官方文档

parse

url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
  • urlString:<String> The URL string to parse.
  • parseQueryString:<Boolean> If true, the query property will always be set to an object returned by the querystring module's parse() method. If false, the query property on the returned URL object will be an unparsed, undecoded string. Defaults to false.
  • slashesDenoteHost:<Boolean> If true, the first token after the literal string // and preceding the next / will be interpreted as the host. For instance, given //foo/bar, the result would be {host: 'foo', pathname: '/bar'} rather than {pathname: '//foo/bar'}. Defaults to false.

【实例】

> url.parse('https://www.google.com/?gws_rd=ssl')
Url {
  protocol: 'https:',//协议
  slashes: true,    //是否有协议的双斜线
  auth: null,
  host: 'www.google.com',   //ip地址,域名
  port: null,   //端口
  hostname: 'www.google.com',   //主机名
  hash: null,   //锚点
  search: '?gws_rd=ssl',    //查询字符串参数
  query: 'gws_rd=ssl',  //发送给服务器的数据
  pathname: '/',    //访问资源的路径名
  path: '/?gws_rd=ssl',     //路径
  href: 'https://www.google.com/?gws_rd=ssl' }

【第二个参数实例演示】随便写的域名

> url.parse('https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1')
# 输出结果
Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'www.zhangdanyang.com:80',
  port: '80',
  hostname: 'www.zhangdanyang.com',
  hash: '#floor1',
  search: '?from=zdy&search=hello',
  query: 'from=zdy&search=hello',
  pathname: '/article/list',
  path: '/article/list?from=zdy&search=hello',
  href: 'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1' }

> url.parse('https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1',true)
# 输出结果
Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'www.zhangdanyang.com:80',
  port: '80',
  hostname: 'www.zhangdanyang.com',
  hash: '#floor1',
  search: '?from=zdy&search=hello',
  query: { from: 'zdy', search: 'hello' },//第二个参数为true时,将query解析为对象
  pathname: '/article/list',
  path: '/article/list?from=zdy&search=hello',
  href: 'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1' }

【第三个参数实例演示】

> url.parse('//www.google.com/',true)
# 输出结果
Url {
  protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: null,
  search: '',
  query: {},
  pathname: '//www.google.com/',
  path: '//www.google.com/',
  href: '//www.google.com/' }

> url.parse('//www.google.com/',true,true)
# 输出结果
Url {
  protocol: null,
  slashes: true,
  auth: null,
  host: 'www.google.com',
  port: null,
  hostname: 'www.google.com',
  hash: null,
  search: '',
  query: {},
  pathname: '/',
  path: '/',
  href: '//www.google.com/' }

format

url.format(urlObject)

【实例】

> url.format({
...   protocol: 'https:',
...   slashes: true,
...   auth: null,
...   host: 'www.zhangdanyang.com:80',
...   port: '80',
...   hostname: 'www.zhangdanyang.com',
...   hash: '#floor1',
...   search: '?from=zdy&search=hello',
...   query: 'from=zdy&search=hello',
...   pathname: '/article/list',
...   path: '/article/list?from=zdy&search=hello',
...   href: 'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1' })
# 输出结果
'https://www.zhangdanyang.com:80/article/list?from=zdy&search=hello#floor1'

resolve

url.resolve(from, to)

【实例】

url.resolve('/one/two/three', 'four')         // '/one/two/four'
url.resolve('http://example.com/', '/one')    // 'http://example.com/one'
url.resolve('http://example.com/one', '/two') // 'http://example.com/two'
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,269评论 19 139
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa阅读 8,949评论 0 6
  • 时间是最宝贵的资源,时间对任何人都是公平的。无论你是成功者还是平凡人,每人每天都拥有同样的24小时。 ...
    江湖人称贾老师阅读 1,607评论 0 48
  • 一改常态,写点八卦想法。 <吴佩慈4年生3胎,男朋友只生不娶,豪门梦越做越尴尬。>还是不能免俗被这样的标题党吸引,...
    彩虹色的猪阅读 210评论 0 1
  • 竹笋这东西,味香质脆,自古代就被视为“菜中珍品”。平民百姓是较难吃到的。为什么呢?太贵! 唐朝诗人李商隐的《初食笋...
    蔡雨山的雪阅读 1,343评论 11 29