溫馨提示×

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

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Node.js中怎么使用URL模塊解析地址

發(fā)布時(shí)間:2021-07-21 10:46:21 來源:億速云 閱讀:112 作者:Leah 欄目:web開發(fā)

這篇文章給大家介紹Node.js中怎么使用URL模塊解析地址,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

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è)上面的一張圖是這樣子的:

Node.js中怎么使用URL模塊解析地址

這張圖解釋了一個(gè)url結(jié)構(gòu)化成哪些部分,哪些部分又包含哪些部分

protocol: 請(qǐng)求協(xié)議

host: URL主機(jī)名已全部轉(zhuǎn)換成小寫, 包括端口信息

auth:URL中身份驗(yàn)證信息部分

hostname:主機(jī)的主機(jī)名部分, 已轉(zhuǎn)換成小寫

port: 主機(jī)的端口號(hào)部分

pathname: URL的路徑部分,位于主機(jī)名之后請(qǐng)求查詢之前

search: URL 的“查詢字符串”部分,包括開頭的問號(hào)。

path: pathname 和 search 連在一起。

query: 查詢字符串中的參數(shù)部分(問號(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 = "//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 = "//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的方式類似于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'

關(guān)于Node.js中怎么使用URL模塊解析地址就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

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

AI