溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

nodeJS之URL

發(fā)布時間:2020-07-10 11:23:35 來源:網(wǎng)絡 閱讀:420 作者:zddnd 欄目:網(wǎng)絡安全

在HTTP部分,詳細介紹了URL的相關(guān)知識。而nodejs中的url模塊提供了一些實用函數(shù),用于URL處理與解析。本文將詳細介紹nodeJS中的URL

 

URL對象

  解析 URL 對象有以下內(nèi)容,依賴于他們是否在 URL 字符串里存在。任何不在 URL 字符串里的部分,都不會出現(xiàn)在解析對象里

'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'

nodeJS之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 "│          ││           │          │      │          │ │              │       │
└──────────┴┴───────────┴──────────┴──────┴──────────┴─┴──────────────┴───────┘

nodeJS之URL

  【href】: 準備解析的完整的 URL,包含協(xié)議和主機(小寫)

'http://user:pass@host.com:8080/p/a/t/h?query=string#hash'

  【protocol】: 請求協(xié)議, 小寫

'http:'

  【slashes】: 協(xié)議要求的斜杠(冒號后)

true 或 false

  【host】: 完整的 URL 小寫 主機部分,包含端口信息

'host.com:8080'

  【auth】: url 中的驗證信息

'user:pass'

  【hostname】: 域名中的小寫主機名

'host.com'

  【port】: 主機的端口號

'8080'

  【pathname】: URL 中的路徑部分,在主機名后,查詢字符前,包含第一個斜杠

'/p/a/t/h'

  【search】: URL 中的查詢字符串,包含開頭的問號

'?query=string'

  【path】: pathname 和 search 連在一起

'/p/a/t/h?query=string'

  【query】: 查詢字符串中得參數(shù)部分,或者使用 querystring.parse() 解析后返回的對象

'query=string' or {'query':'string'}

  【hash】: URL 的 “#” 后面部分(包括 # 符號)

'#hash'

 

URL方法

  URL模塊包含分析和解析 URL 的工具。調(diào)用 require('url') 來訪問模塊

nodeJS之URL

var url = require('url');/*{ parse: [Function: urlParse],
  resolve: [Function: urlResolve],
  resolveObject: [Function: urlResolveObject],
  format: [Function: urlFormat],
  Url: [Function: Url] } */console.log(url);

nodeJS之URL

【url.parse(urlStr[, parseQueryString][, slashesDenoteHost])】

  輸入 URL 字符串,返回一個對象

  第二個參數(shù)parseQueryString(默認為false),如為false,則urlObject.query為未解析的字符串,比如author=%E5%B0%8F%E7%81%AB%E6%9F%B4,且對應的值不會decode;如果parseQueryString為true,則urlObject.query為object,比如{ author: '小火柴' },且值會被decode

  第三個參數(shù)slashesDenoteHos(默認為false),如果為true,可以正確解析不帶協(xié)議頭的URL,類似//foo/bar里的foo就會被認為是hostname;如果為false,則foo被認為是pathname的一部分

nodeJS之URL

var url = require('url');var str = 'http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash';/*Url {
  protocol: 'http:',
  slashes: true,
  auth: 'user:pass',
  host: 'host.com:8080',
  port: '8080',
  hostname: 'host.com',
  hash: '#hash',
  search: '?author=%E5%B0%8F%E7%81%AB%E6%9F%B4',
  query: 'author=%E5%B0%8F%E7%81%AB%E6%9F%B4',
  pathname: '/p/a/t/h',
  path: '/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4',
  href: 'http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash' } */console.log(url.parse(str));

nodeJS之URL

nodeJS之URL

var url = require('url');var str = 'http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash';/*Url {
  protocol: 'http:',
  slashes: true,
  auth: 'user:pass',
  host: 'host.com:8080',
  port: '8080',
  hostname: 'host.com',
  hash: '#hash',
  search: '?author=%E5%B0%8F%E7%81%AB%E6%9F%B4',
  query: { author: '小火柴' },
  pathname: '/p/a/t/h',
  path: '/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4',
  href: 'http://user:pass@host.com:8080/p/a/t/h?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hash' }  */console.log(url.parse(str,true));

nodeJS之URL

nodeJS之URL

var url = require('url');var str = '//foo/bar';var result1 = url.parse(str,true);var result2 = url.parse(str,true,true);
console.log(result1.path);//'//foo/bar'console.log(result1.pathname);//'//foo/bar'console.log(result1.hostname);//nullconsole.log(result2.path);//'/bar'console.log(result2.pathname);//'/bar'console.log(result2.hostname);//'foo'

nodeJS之URL

【url.format(urlObject)】

  url.parse(str)的反向操作,輸入一個解析過的 URL 對象,返回格式化過的字符串

  urlObject包含了很多字段,比如protocol、slashes、protocol等,且不一定需要全部傳,所以有一套解析邏輯

  格式化的工作流程如下

nodeJS之URL

href 會被忽略
protocol 無論是否有末尾的 : (冒號),會同樣的處理
http, https, ftp, gopher, file 協(xié)議會被添加后綴://mailto, xmpp, aim, sftp, foo, 等協(xié)議添加后綴:
slashes 如果協(xié)議需要 ://,設(shè)置為 true僅需對之前列出的沒有斜杠的協(xié)議,比如議 mongodb://localhost:8000/auth 如果出現(xiàn)將會使用.
hostname 僅在缺少 host 時使用
port 僅在缺少 host 時使用
host 用來替換 hostname 和 port
pathname 無論結(jié)尾是否有 / 將會同樣處理
search 將會替代 query屬性
無論前面是否有 / 將會同樣處理
query (對象; 參見 querystring) 如果沒有 search,將會使用
hash 無論前面是否有#,都會同樣處理

nodeJS之URL

nodeJS之URL

var url = require('url');var obj = {
  protocol: 'http:',
  auth: 'user:pass',
  host: 'host.com:8080',
  hash: '#hash',
  query: { author: '小火柴' }
}//http://user:pass@host.com:8080?author=%E5%B0%8F%E7%81%AB%E6%9F%B4#hashconsole.log(url.format(obj));

nodeJS之URL

【url.resolve(from, to)】

  url.resolve()方法以一種瀏覽器解析超鏈接的方式把一個目標URL解析成相對于一個基礎(chǔ)URL,參數(shù)如下

from <String> 解析時相對的基本 URL。
to <String> 要解析的超鏈接 URL。
var url = require('url');
console.log(url.resolve('/one/two/three', 'four'));         // '/one/two/four'console.log(url.resolve('http://example.com/', '/one'));    // 'http://example.com/one'console.log(url.resolve('http://example.com/one', '/two')); // 'http://example.com/two'


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

js j %d
AI