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

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

在憑祥等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都做網(wǎng)站、成都網(wǎng)站制作 網(wǎng)站設(shè)計制作按需網(wǎng)站建設(shè),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營銷,外貿(mào)網(wǎng)站建設(shè),憑祥網(wǎng)站建設(shè)費用合理。

url結(jié)構(gòu)化/模塊化/路徑解析

  • 結(jié)構(gòu)化:url.parse(urlString[, parseQueryString[, slashesDenoteHost]])

  • 模塊化:url.format(urlObject)

  • 路徑解析:url.resolve(from, to)

一個URL字符串是一個結(jié)構(gòu)化的字符串包含多個有意義的組件。在解析時,返回一個URL對象包含每一個組件的屬性。

官方手冊上面的一張圖是這樣子的:

Node.js中怎么使用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ù)默認為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 = "//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模塊化

將一個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()方法解決了目標URL相對于基本URL的方式類似于Web瀏覽器解決錨標記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'

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

標題名稱:Node.js中怎么使用URL模塊解析地址
文章網(wǎng)址:http://muchs.cn/article24/iehije.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司、網(wǎng)站建設(shè)、微信小程序、虛擬主機網(wǎng)站設(shè)計、微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)