Node.js 官方URL模块简介

发布时间:2019-06-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Node.js 官方URL模块简介脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

URL

这个模块含有一些系列方法函数处理和解析URL
使用require('url')使用

parsing(string)

这个方法返回包含具体路由信息的对象
没有就返回null

url.parse('http://user:pass@host.com:8080/p/a/t/h?query=string')
{
  protocol: 'http:',
  slashes: true,
  auth: 'user:pass',
  host: 'host.com:8080',
  port: '8080',
  hostname: 'host.com',
  hash: null,
  search: '?query=string',
  query: 'query=string',
  pathname: '/p/a/t/h',
  path: '/p/a/t/h?query=string',
  href: 'http://user:pass@host.com:8080/p/a/t/h?query=string#hash' 
}
  1. href,完整的路径

  2. pathname,路径名字不包含参数

  3. host比hostname多一个端口号

format(urlObj)

同理这个函数就是根据obj的信息构造一个路径

var obj =
{ protocol: 'https',
  host: 'www.cycok.com:4000',
  pathname: 'index' 
}
url.format(obj)
//returns 'https://www.cycok.com:4000/index'

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'

脚本宝典总结

以上是脚本宝典为你收集整理的Node.js 官方URL模块简介全部内容,希望文章能够帮你解决Node.js 官方URL模块简介所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: