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閱讀 134,837評論 18 139
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 8,900評論 0 6
  • 時間是最寶貴的資源,時間對任何人都是公平的。無論你是成功者還是平凡人,每人每天都擁有同樣的24小時。 ...
    江湖人稱賈老師閱讀 1,591評論 0 48
  • 一改常態,寫點八卦想法。 <吳佩慈4年生3胎,男朋友只生不娶,豪門夢越做越尷尬。>還是不能免俗被這樣的標題黨吸引,...
    彩虹色的豬閱讀 200評論 0 1
  • 竹筍這東西,味香質脆,自古代就被視為“菜中珍品”。平民百姓是較難吃到的。為什么呢?太貴! 唐朝詩人李商隱的《初食筍...
    蔡雨山的雪閱讀 1,285評論 11 29