Node.js學(xué)習(xí)之地址解析模塊URL的使用詳解
前言
本文主要給大家介紹了關(guān)于Node.js地址解析模塊URL使用的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
url結(jié)構(gòu)化/模塊化/路徑解析
- 結(jié)構(gòu)化:
url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
- 模塊化:
url.format(urlObject)
- 路徑解析:
url.resolve(from, to)
一個(gè)URL字符串是一個(gè)結(jié)構(gòu)化的字符串包含多個(gè)有意義的組件。在解析時(shí),返回一個(gè)URL對(duì)象包含每一個(gè)組件的屬性。
官方手冊(cè)上面的一張圖是這樣子的:
這張圖解釋了一個(gè)url結(jié)構(gòu)化成哪些部分,哪些部分又包含哪些部分
protocol: 請(qǐng)求協(xié)議
host: URL主機(jī)名已全部轉(zhuǎn)換成小寫(xiě), 包括端口信息
auth:URL中身份驗(yàn)證信息部分
hostname:主機(jī)的主機(jī)名部分, 已轉(zhuǎn)換成小寫(xiě)
port: 主機(jī)的端口號(hào)部分
pathname: URL的路徑部分,位于主機(jī)名之后請(qǐng)求查詢(xún)之前
search: URL 的“查詢(xún)字符串”部分,包括開(kāi)頭的問(wèn)號(hào)。
path: pathname 和 search 連在一起。
query: 查詢(xún)字符串中的參數(shù)部分(問(wèn)號(hào)后面部分字符串),或者使用 querystring.parse() 解析后返回的對(duì)象。
hash: URL 的 “#” 后面部分(包括 # 符號(hào))
url結(jié)構(gòu)化
將一個(gè)url地址結(jié)構(gòu)化成為擁有上圖屬性的url對(duì)象。url.parse第二個(gè)和第三個(gè)參數(shù)默認(rèn)為false。
- 第二個(gè)參數(shù)決定query屬性值是字符串還是對(duì)象
- 第三個(gè)參數(shù)如果為true,//后的第一個(gè)令牌文字字符串和下一個(gè)/之間的文字字符串將被解釋為主機(jī)
例子如下
const url = require("url"); var urlstr = "http://localhost:8888/bb?name=bigbear&memo=helloworld&memo=helloC"; var urlobj = url.parse(urlstr); console.log(urlobj); /* Url { protocol: 'http:', slashes: true, auth: null, host: 'localhost:8888', port: '8888', hostname: 'localhost', hash: null, search: '?name=bigbear&memo=helloworld&memo=helloC', query: 'name=bigbear&memo=helloworld&memo=helloC', pathname: '/bb', path: '/bb?name=bigbear&memo=helloworld&memo=helloC', href: 'http://localhost:8888/bb?name=bigbear&memo=helloworld&memo=helloC' } */
第二個(gè)參數(shù)為true時(shí)
query: { name: ‘bigbear', memo: [ ‘helloworld', ‘helloC' ] },
例子如下:
const url = require("url"); var urlstr = "http://localhost:8888/bb?name=bigbear&memo=helloworld&memo=helloC"; console.log( url.parse(urlstr, true) ) /* Url { protocol: 'http:', slashes: true, auth: null, host: 'localhost:8888', port: '8888', hostname: 'localhost', hash: null, search: '?name=bigbear&memo=helloworld&memo=helloC', query: { name: 'bigbear', memo: [ 'helloworld', 'helloC' ] }, pathname: '/bb', path: '/bb?name=bigbear&memo=helloworld&memo=helloC', href: 'http://localhost:8888/bb?name=bigbear&memo=helloworld&memo=helloC' } */
第三個(gè)參數(shù)對(duì)比
例子如下:
const url = require("url"); var urlstr = "http://foo/bar "; console.log( url.parse(urlstr, true,true) ) /* 輸出:Url { protocol: null, slashes: true, auth: null, host: 'foo', port: null, hostname: 'foo', hash: null, search: '', query: {}, pathname: '/bar', path: '/bar', href: '//foo/bar' } */ const url = require("url"); var urlstr = "http://foo/bar "; console.log( url.parse(urlstr) ) /* 輸出: Url { protocol: null, slashes: null, auth: null, host: null, port: null, hostname: null, hash: null, search: null, query: null, pathname: '//foo/bar', path: '//foo/bar', href: '//foo/bar' } */
url模塊化
將一個(gè)url對(duì)象轉(zhuǎn)換成一個(gè)url字符串,url對(duì)象中的屬性為url.parse()
產(chǎn)生的對(duì)象的屬性。
url.parse()
和url.format()
互為逆操作。
例子如下:
const url = require("url"); var Urlobj = { protocol: 'http:', slashes: true, auth: null, host: 'localhost:8888', port: '8888', hostname: 'localhost', hash: null, search: '?name=bigbear&memo=helloworld&memo=helloC', query: { name: 'bigbear', memo: [ 'helloworld', 'helloC' ] }, pathname: '/bb', path: '/bb?name=bigbear&memo=helloworld&memo=helloC', } console.log( url.format(Urlobj) ) //輸出:http://localhost:8888/bb?name=bigbear&memo=helloworld&memo=helloC
路徑解析:url.resolve(from, to)
url.resolve()
方法解決了目標(biāo)URL相對(duì)于基本URL的方式類(lèi)似于Web瀏覽器解決錨標(biāo)記href。
官方手冊(cè)例子:
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'
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Node.js與MySQL交互操作及其注意事項(xiàng)
這篇文章給大家主要介紹了Node.js與MySQL交互操作及其注意事項(xiàng),非常的詳細(xì),有相同需求的小伙伴可以參考下2016-10-10Node中文件斷點(diǎn)續(xù)傳原理和方法總結(jié)
在之前做過(guò)一個(gè)小項(xiàng)目,涉及到了文件上傳,在大文件上面使用了斷點(diǎn)續(xù)傳,降低了服務(wù)器方面的壓力,現(xiàn)在小編把Node中文件斷點(diǎn)續(xù)傳原理和方法總結(jié)分享給大家,感興趣的朋友一起看看吧2022-01-01Node.js中文件操作模塊File System的詳細(xì)介紹
FileSystem模塊是類(lèi)似UNIX(POSIX)標(biāo)準(zhǔn)的文件操作API,用于操作文件系統(tǒng)——讀寫(xiě)目錄、讀寫(xiě)文件——Node.js底層使用C程序來(lái)實(shí)現(xiàn),這些功能是客戶(hù)端JS所不具備的。下面這篇文章就給大家詳細(xì)介紹了Node.js中的文件操作模塊File System,有需要的朋友們可以參考借鑒。2017-01-01node.js中的fs.realpathSync方法使用說(shuō)明
這篇文章主要介紹了node.js中的fs.realpathSync方法使用說(shuō)明,本文介紹了fs.realpathSync的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12手把手教你使用TypeScript開(kāi)發(fā)Node.js應(yīng)用
為了減少代碼編寫(xiě)過(guò)程中出現(xiàn)的錯(cuò)誤,以及更好的維護(hù)你的項(xiàng)目,本文將手把手教你配置一個(gè)簡(jiǎn)單的開(kāi)發(fā)環(huán)境來(lái)編寫(xiě)Node.js的應(yīng)用程序,感興趣的小伙伴們可以參考一下2019-05-05nodejs使用Express框架寫(xiě)后端接口的全過(guò)程
最近學(xué)習(xí)了基于前后端分離的開(kāi)發(fā)模式,我前端使用Vue框架,后端使用nodejs開(kāi)發(fā)API接口,下面這篇文章主要給大家介紹了關(guān)于nodejs使用Express框架寫(xiě)后端接口的相關(guān)資料,需要的朋友可以參考下2022-05-05一文詳解Node中module.exports和exports區(qū)別
這篇文章主要介紹了一文詳解Node中module.exports和exports區(qū)別2023-03-03Node.js中使用Log.io在瀏覽器中實(shí)時(shí)監(jiān)控日志(等同tail -f命令)
這篇文章主要介紹了Node.js中使用Log.io在瀏覽器中實(shí)時(shí)監(jiān)控日志,Log.io等同于tail -f命令,但更強(qiáng)大,需要的朋友可以參考下2014-09-09