url結構化/模塊化/路徑解析
結構化:url.parse(urlString[, parseQueryString[, slashesDenoteHost]])
模塊化:url.format(urlObject)
路徑解析:url.resolve(from, to)
一個URL字符串是一個結構化的字符串包含多個有意義的組件。在解析時,返回一個URL對象包含每一個組件的屬性。
官方手冊上面的一張圖是這樣子的:
這張圖解釋了一個url結構化成哪些部分,哪些部分又包含哪些部分
protocol: 請求協議
host: URL主機名已全部轉換成小寫, 包括端口信息
auth:URL中身份驗證信息部分
hostname:主機的主機名部分, 已轉換成小寫
port: 主機的端口號部分
pathname: URL的路徑部分,位於主機名之後請求查詢之前
search: URL 的“查詢字符串”部分,包括開頭的問號。
path: pathname 和 search 連在一起。
query: 查詢字符串中的參數部分(問號後面部分字符串),或者使用 querystring.parse() 解析後返回的對象。
hash: URL 的 “#” 後面部分(包括 # 符號)
url結構化
將一個url地址結構化成爲擁有上圖屬性的url對象。url.parse第二個和第三個參數默認爲false。
- 第二個參數決定query屬性值是字符串還是對象
- 第三個參數如果爲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' }
*/
第二個參數爲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' }
*/
第三個參數對比
例子如下:
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對象轉換成一個url字符串,url對象中的屬性爲url.parse()產生的對象的屬性。
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'