url
模塊和querystring
模塊是非常重要的兩個(gè)URL
處理模塊。在做node
服務(wù)端的開(kāi)發(fā)時(shí)會(huì)經(jīng)常用到。
url
在介紹url
模塊之前我們先來(lái)一張圖,看懂了這張圖對(duì)于url
這個(gè)模塊你就基本上沒(méi)什么問(wèn)題了。
我們來(lái)解釋下各自的含義
- protocol:協(xié)議,需要注意的是包含了
:
,并且是小寫的?!鞠嚓P(guān)教程推薦:nodejs視頻教程、編程教學(xué)】 - slashes:如果
:
后面跟了兩個(gè)//
,那么為true。 - auth:認(rèn)證信息,如果有密碼,為
usrname:passwd
,如果沒(méi)有,則為usrname
。注意,這里區(qū)分大小寫。 - host:主機(jī)名。注意包含了端口,比如
ke.qq.com:8080
,并且是小寫的。 - hostname:主機(jī)名,不包含端口,并且是小寫的。
- port: 端口號(hào)。
- path:路徑部分,包含search部分。
- pathname:路徑部分,不包含search部分。
- search:查詢字符串,注意,包含了
?
,此外,值是沒(méi)有經(jīng)過(guò)decode的。 - query:字符串 或者 對(duì)象。如果是字符串,則是
search
去掉?
,其余一樣;如果是對(duì)象,那么是decode過(guò)的。 - hash:哈希部分,注意包含了
#
。 - href:原始的地址。不過(guò)需要注意的是,
protocol
、host
會(huì)被轉(zhuǎn)成小寫字母。
下面我們來(lái)講解下它的三個(gè)常用方法
parse(urlString, parseQueryString, slashesDenoteHost)
該方法將url
字符串,解析成object
,便于開(kāi)發(fā)者進(jìn)行操作。
const url = require("url"); const str = "http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1"; const obj = url.parse(str); console.log(obj);
輸出
該方法還支持傳遞另外兩個(gè)參數(shù),parseQueryString
和slashesDenoteHos
parseQueryString:(默認(rèn)為false)如為false
,則urlObject.query
為未解析的字符串,比如nick=%E4%B8%AD%E6%96%87
,且對(duì)應(yīng)的值不會(huì)decode
;如果parseQueryString
為true,則urlObject.query
為object
,比如{ nick: '中文' }
,且值會(huì)被`decode;
const url = require("url"); const str = "http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1"; const obj2 = url.parse(str, true); console.log(obj2);
slashesDenoteHos:(默認(rèn)為false)如果為true
,那么類似//randy/nick
里的randy
就會(huì)被認(rèn)為是hostname
;如果為false
,則randy
被認(rèn)為是pathname
的一部分。
光看起來(lái)可能不太理解這句話的含義,下面筆者舉個(gè)例子我相信你們就明白了。
const str2 = "//randy/nick"; const obj3 = url.parse(str2, true, false); console.log(obj3); const obj4 = url.parse(str2, true, true); console.log(obj4);
format(urlObject)
這個(gè)方法就是parse
的反向操作。將對(duì)象轉(zhuǎn)成url
字符串。
const pathObj = { protocol: "http:", slashes: true, auth: "user:password", host: "randy.com:8080", port: "8080", hostname: "randy.com", hash: "#part=1", search: "?nick=%E4%B8%AD%E6%96%87", query: "nick=%E4%B8%AD%E6%96%87", pathname: "/index.html", path: "/index.html?nick=%E4%B8%AD%E6%96%87", href: "http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1", }; console.log(url.format(pathObj)); // http://user:password@randy.com:8080/index.html?nick=%E4%B8%AD%E6%96%87#part=1
resolve(from, to)
該方法用于解析相對(duì)于基本URL
的目標(biāo)URL
。
console.log(url.resolve("/one/two/three", "four")); // /one/two/four console.log(url.resolve("http://example.com/", "/one")); // http://example.com/one console.log(url.resolve("http://example.com/one", "/two")); // http://example.com/two console.log(url.resolve("http://example.com/one/ddd/ddd/ddd", "./two")); // http://example.com/one/ddd/ddd/two console.log(url.resolve("http://example.com/one/ddd/ddd/ddd", "../two")); // http://example.com/one/ddd/two console.log(url.resolve("http://example.com/one/ddd/ddd/ddd", ".../two")); // http://example.com/one/ddd/ddd/.../two
querystring
querystring
這個(gè)模塊,也是用來(lái)做url
查詢參數(shù)的解析。這里我們重點(diǎn)分析下它的parse
和stringify
兩個(gè)方法。
parse(str, sep, eq, options)
parse
是將查詢字符串轉(zhuǎn)成對(duì)象類型,并且也會(huì)decode
。
const querystring = require("querystring"); const str = "nick=randy&age=24&nick2=%E4%B8%AD%E6%96%87"; const obj = querystring.parse(str); console.log(obj); // { nick: 'randy', age: '24', nick2: '中文' }
下面我們?cè)賮?lái)看看它的第二和第三個(gè)參數(shù)。其實(shí)相當(dāng)于可以替換&、=
為自定義字符,下面筆者舉個(gè)例子就很快明白了。
const str1 = "name-randy|country-cn"; const obj1 = querystring.parse(str1); console.log(obj1); // { 'name-randy|country-cn': '' } const obj2 = querystring.parse(str1, "|", "-"); console.log(obj2); // { name: 'randy', country: 'cn' }
相當(dāng)于把&
替換成了|
,把=
替換成了-
。筆者感覺(jué)配到這種情況應(yīng)該不多。
stringify(obj, sep, eq, options)
這個(gè)方法就是上面parse
的反向操作。下面咱們直接上例子
const obj3 = { nick: "randy", age: "24", }; const str4 = querystring.stringify(obj3); console.log(str4); // nick=randy&age=24
這個(gè)方法也是支持自定義分割符的。
const obj5 = { name: "randy", country: "cn", }; const str6 = querystring.stringify(obj5, "|", "-"); console.log(str6); // name-randy|country-c