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)
一個URL字符串是一個結(jié)構(gòu)化的字符串包含多個有意義的組件。在解析時,返回一個URL對象包含每一個組件的屬性。
官方手冊上面的一張圖是這樣子的:

這張圖解釋了一個url結(jié)構(gòu)化成哪些部分,哪些部分又包含哪些部分
protocol: 請求協(xié)議
host: URL主機名已全部轉(zhuǎn)換成小寫, 包括端口信息
auth:URL中身份驗證信息部分
hostname:主機的主機名部分, 已轉(zhuǎn)換成小寫
port: 主機的端口號部分
pathname: URL的路徑部分,位于主機名之后請求查詢之前
search: URL 的“查詢字符串”部分,包括開頭的問號。
path: pathname 和 search 連在一起。
query: 查詢字符串中的參數(shù)部分(問號后面部分字符串),或者使用 querystring.parse() 解析后返回的對象。
hash: URL 的 “#” 后面部分(包括 # 符號)
url結(jié)構(gòu)化
將一個url地址結(jié)構(gòu)化成為擁有上圖屬性的url對象。url.parse第二個和第三個參數(shù)默認(rèn)為false。
- 第二個參數(shù)決定query屬性值是字符串還是對象
- 第三個參數(shù)如果為true,//后的第一個令牌文字字符串和下一個/之間的文字字符串將被解釋為主機
例子如下
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' }
*/
第二個參數(shù)為true時
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' }
*/
第三個參數(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模塊化
將一個url對象轉(zhuǎn)換成一個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í)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Node.js中文件操作模塊File System的詳細(xì)介紹
FileSystem模塊是類似UNIX(POSIX)標(biāo)準(zhǔn)的文件操作API,用于操作文件系統(tǒng)——讀寫目錄、讀寫文件——Node.js底層使用C程序來實現(xiàn),這些功能是客戶端JS所不具備的。下面這篇文章就給大家詳細(xì)介紹了Node.js中的文件操作模塊File System,有需要的朋友們可以參考借鑒。2017-01-01
node.js中的fs.realpathSync方法使用說明
這篇文章主要介紹了node.js中的fs.realpathSync方法使用說明,本文介紹了fs.realpathSync的方法說明、語法、接收參數(shù)、使用實例和實現(xiàn)源碼,需要的朋友可以參考下2014-12-12
手把手教你使用TypeScript開發(fā)Node.js應(yīng)用
為了減少代碼編寫過程中出現(xiàn)的錯誤,以及更好的維護你的項目,本文將手把手教你配置一個簡單的開發(fā)環(huán)境來編寫Node.js的應(yīng)用程序,感興趣的小伙伴們可以參考一下2019-05-05
一文詳解Node中module.exports和exports區(qū)別
這篇文章主要介紹了一文詳解Node中module.exports和exports區(qū)別2023-03-03
Node.js中使用Log.io在瀏覽器中實時監(jiān)控日志(等同tail -f命令)
這篇文章主要介紹了Node.js中使用Log.io在瀏覽器中實時監(jiān)控日志,Log.io等同于tail -f命令,但更強大,需要的朋友可以參考下2014-09-09

