Node.js學(xué)習(xí)之地址解析模塊URL的使用詳解
前言
本文主要給大家介紹了關(guān)于Node.js地址解析模塊URL使用的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(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對象包含每一個(gè)組件的屬性。
官方手冊上面的一張圖是這樣子的:
這張圖解釋了一個(gè)url結(jié)構(gòu)化成哪些部分,哪些部分又包含哪些部分
protocol: 請求協(xié)議
host: URL主機(jī)名已全部轉(zhuǎn)換成小寫, 包括端口信息
auth:URL中身份驗(yàn)證信息部分
hostname:主機(jī)的主機(jī)名部分, 已轉(zhuǎn)換成小寫
port: 主機(jī)的端口號部分
pathname: URL的路徑部分,位于主機(jī)名之后請求查詢之前
search: URL 的“查詢字符串”部分,包括開頭的問號。
path: pathname 和 search 連在一起。
query: 查詢字符串中的參數(shù)部分(問號后面部分字符串),或者使用 querystring.parse() 解析后返回的對象。
hash: URL 的 “#” 后面部分(包括 # 符號)
url結(jié)構(gòu)化
將一個(gè)url地址結(jié)構(gòu)化成為擁有上圖屬性的url對象。url.parse第二個(gè)和第三個(gè)參數(shù)默認(rèn)為false。
- 第二個(gè)參數(shù)決定query屬性值是字符串還是對象
- 第三個(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ù)對比
例子如下:
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對象轉(zhuǎn)換成一個(gè)url字符串,url對象中的屬性為url.parse()
產(chǎn)生的對象的屬性。
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相對于基本URL的方式類似于Web瀏覽器解決錨標(biāo)記href。
官方手冊例子:
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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Node.js與MySQL交互操作及其注意事項(xiàng)
這篇文章給大家主要介紹了Node.js與MySQL交互操作及其注意事項(xiàng),非常的詳細(xì),有相同需求的小伙伴可以參考下2016-10-10Node中文件斷點(diǎn)續(xù)傳原理和方法總結(jié)
在之前做過一個(gè)小項(xiàng)目,涉及到了文件上傳,在大文件上面使用了斷點(diǎn)續(xù)傳,降低了服務(wù)器方面的壓力,現(xiàn)在小編把Node中文件斷點(diǎn)續(xù)傳原理和方法總結(jié)分享給大家,感興趣的朋友一起看看吧2022-01-01Node.js中文件操作模塊File System的詳細(xì)介紹
FileSystem模塊是類似UNIX(POSIX)標(biāo)準(zhǔn)的文件操作API,用于操作文件系統(tǒng)——讀寫目錄、讀寫文件——Node.js底層使用C程序來實(shí)現(xiàn),這些功能是客戶端JS所不具備的。下面這篇文章就給大家詳細(xì)介紹了Node.js中的文件操作模塊File System,有需要的朋友們可以參考借鑒。2017-01-01node.js中的fs.realpathSync方法使用說明
這篇文章主要介紹了node.js中的fs.realpathSync方法使用說明,本文介紹了fs.realpathSync的方法說明、語法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下2014-12-12手把手教你使用TypeScript開發(fā)Node.js應(yīng)用
為了減少代碼編寫過程中出現(xiàn)的錯(cuò)誤,以及更好的維護(hù)你的項(xiàng)目,本文將手把手教你配置一個(gè)簡單的開發(fā)環(huán)境來編寫Node.js的應(yīng)用程序,感興趣的小伙伴們可以參考一下2019-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