url
??? querystring
??? ? ?? ?? ??? URL
?? ?????. node
??? ??? ? ?? ?????. url
模塊和querystring
模塊是非常重要的兩個URL
處理模塊。在做node
服務端的開發(fā)時會經(jīng)常用到。
url
在介紹url
模塊之前我們先來一張圖,看懂了這張圖對于url
這個模塊你就基本上沒什么問題了。
我們來解釋下各自的含義
- protocol:協(xié)議,需要注意的是包含了
:
,并且是小寫的?!鞠嚓P教程推薦:nodejs視頻教程、編程教學】 - slashes:如果
:
后面跟了兩個//
,那么為true。 - auth:認證信息,如果有密碼,為
usrname:passwd
,如果沒有,則為usrname
。注意,這里區(qū)分大小寫。 - host:主機名。注意包含了端口,比如
ke.qq.com:8080
,并且是小寫的。 - hostname:主機名,不包含端口,并且是小寫的。
- port: 端口號。
- path:路徑部分,包含search部分。
- pathname:路徑部分,不包含search部分。
- search:查詢字符串,注意,包含了
?
,此外,值是沒有經(jīng)過decode的。 - query:字符串 或者 對象。如果是字符串,則是
search
去掉?
,其余一樣;如果是對象,那么是decode過的。 - hash:哈希部分,注意包含了
#
。 - href:原始的地址。不過需要注意的是,
protocol
、host
會被轉成小寫字母。
下面我們來講解下它的三個常用方法
parse(urlString, parseQueryString, slashesDenoteHost)
該方法將url
字符串,解析成object
,便于開發(fā)者進行操作。
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);
輸出
該方法還支持傳遞另外兩個參數(shù),parseQueryString
和slashesDenoteHos
parseQueryString:(默認為false)如為false
,則urlObject.query
為未解析的字符串,比如nick=%E4%B8%AD%E6%96%87
,且對應的值不會decode
;如果parseQueryString
為true,則urlObject.query
為object
,比如{ nick: '中文' }
,且值會被`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:(默認為false)如果為true
,那么類似//randy/nick
里的randy
就會被認為是hostname
;如果為false
,則randy
被認為是pathname
的一部分。
光看起來可能不太理解這句話的含義,下面筆者舉個例子我相信你們就明白了。
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)
這個方法就是parse
的反向操作。將對象轉成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)
該方法用于解析相對于基本URL
的目標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
這個模塊,也是用來做url
查詢參數(shù)的解析。這里我們重點分析下它的parse
和stringify
兩個方法。
parse(str, sep, eq, options)
parse
是將查詢字符串轉成對象類型,并且也會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: '中文' }
下面我們再來看看它的第二和第三個參數(shù)。其實相當于可以替換&、=
為自定義字符,下面筆者舉個例子就很快明白了。
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' }
相當于把&
替換成了|
,把=
替換成了-
。筆者感覺配到這種情況應該不多。
stringify(obj, sep, eq, options)
這個方法就是上面parse
url
url
??? ???? ?? ? ??? url
? ?? ??? ???? ???? ?? ??? ?? ?????. ????? ? ???? ??? ??? ????. 
- ????: ????
:
? ???? ??? ????? ?? ???? ???. [?? ?? ????: nodejs ??? ????, ????? ??] - ???:
:
? ??? ?? by//
? 2? ??? true???. - auth: ?? ???, ????? ???
usrname:passwd
, ???usrname
???. ????? ????? ?? ?????. - ???: ??? ??. ???
ke.qq.com:8080
? ?? ???? ??????. - ??? ??: ??? ???? ??? ???? ??? ??????.
- ??: ?? ??.
- ??: ?? ??? ??? ?? ?????.
- ???: ?? ??, ?? ?? ??.
- ??: ?? ?????
?
? ???? ????. ?? ?? ????? ????. - ??: ??? ?? ??. ?????
search
???
?? ???? ???? ???? ??????. - hash: ?? ??,
#
? ???? ????. - href: ?? ??. ?,
protocol
?host
? ???? ????? ????? ????.
parse(urlString, parseQueryString, slashesDenoteHost) h4>??? ?? url
???? ???? ???? ??? object
? ?? ?????. ??const obj3 = {
nick: "randy",
age: "24",
};
const str4 = querystring.stringify(obj3);
console.log(str4); // nick=randy&age=24
??Output????
????? ???? ? ?? ?? ????? parseQueryString
? slashesDenoteHos
????parseQueryString ??? ?????. false
? ?? urlObject.query
? nick=%E4%B8%AD%E6%96%87
? ?? ?? ???? ?? ????? ??? ?????. ?? ???
?? ????. parseQueryString
? true? ?? urlObject.query
? ??
???. > { nick: '???' }, ?? `??????. ??const obj5 = {
name: "randy",
country: "cn",
};
const str6 = querystring.stringify(obj5, "|", "-");
console.log(str6); // name-randy|country-c
??
????slashesDenoteHos: (???? false) true
? ?? ? ?? >/ /randy/nick
? randy
? ??? ??
?? ?????. false
?? randy< /code> code>? <code>pathname
? ??? ?????. ?????? ?? ? ??? ??? ???? ?? ?? ????. ?? ?? ????? ???? ?? ????. ??rrreee??
??< h4 data-id="heading-3">format(urlObject)
??? ???? ??
? ?? ?????. ??? url
???? ?????. ??rrreeeresolve(from, to)
??? ??? ??URL</code? ???? ??? ???? ? ?????. > <code>URL
. ??rrreeequerystring
??querystring
? ??? url
?? ????? ?? ???? ??? ?????. ???? parse
? stringify
?? ? ?? ??? ???? ? ??? ???. ??parse(str, sep, eq, options)
??parse
? ?? ???? ?? ???? ?????. , ???
? ?????. ??rrreee?? ? ??? ? ?? ????? ???????. ??? & ? =
? ??? ?? ??? ??? ?? ????. ???? ??? ?? ???? ??? ??? ? ????. ??rrreee??&
? |
? ???, =
? -
? ??? ?? ????. ??? ?? ??? ???? ??? ?????. ??stringify(obj, sep, eq, options)
??? ??? ???? ??
?????. ?? ??? ?? ?????????rrreee??? ??? ?? ?? ??? ?????. ??const obj5 = { name: &quot;randy&quot;, country: &quot;cn&quot;, }; const str6 = querystring.stringify(obj5, &quot;|&quot;, &quot;-&quot;); console.log(str6); // name-randy|country-c
更多node相關知識,請訪問:nodejs 教程!
? ??? Node? url ??? querystring ??? ?? ???? ?????.? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

PHP? Vue: ????? ?? ??? ??? ?? ??? ???? ??? ???? ??? ????? ??? ?? ? ????? ????. ???? ? ??? ? ?????? ??? ?? ?? ??? ?? ? ???? ?? ??? ?? ???? ?? ????? ??? ??? ???? ??? ? ??? ?????? ???? ???. ????? ?? ??? ? ?? ??? ??? PHP? Vue.js? ?? ???? ??? ???? ? ? ????. ? ????? ??? ? ? ??? ? ? ???? ??? ? ??? PHP? Vue? ??? ??? ?? ??? ???????.

??? ???? ????? ??? Go ??? ??? ?? ???? ?? ?????. ??? Go ??? ????? ??? ????? ??? ?? ????. ??? ????? ??? Go ??? ???? ???? ??? ?? ??? ????? ??? ??? ??? ?? ????. ? ????? ????? ??? Go ??? ??? ? ?? ???? ???? ??? ? ??? ? ? ??? ? ??? ???? ?? ??? ?????. ???? ????? ????? ??? ?????? ???? ?? JavaScript, HTML, CSS? ???? ??? ????.

Django? ?? ??? ??? ????? ???? Python?? ??? ? ?????? ????????. Django? ? ???????? Django? ??????? ?????? ?? ??? ???? ?????? ???? ??? ?? ?? ??? ?????. ?????? ???? ?? ?????? ?????? ????, ???? HTTP ????? ?? ???? ?????? ??? ????? ?????. ?????? ???? ???? ?????? ??? ????? ????? ???? ?? ???? ??? ????? ??, ??? ??? ??? ? ????.

C# ????? ??? ?? ???? ????? ?????? ??? ??? ?????. ??? ???? ????? ???? ???? ?? ?????? ???? ?? ??? ?? ? ????? ???????. ? ????? C# ???? ?? ??? ?? ????? ???? ? ??? ?? ? ?? ??? ?? ? ??? ?? ?? ??? ?????. ????? ??? ??? ? ?????? ???? ?? ??? API ?????? ?? ??? ??? ? ????. ?????? ??? ?? ??? ???? ???? ???? ?? ??? ?? ?? ????? ??? ???? ????. ????? ???? ????? ??? ?????.

????? ?? ????? ???? ??? HTML/CSS ??, JavaScript ??, ????? ? ?????, ???? ??, ???? ? ??? ??, ?? ???, ??? ??? ??, ????? ?????, ??? ??, ??? ?? ? ???. ??? ??? ???? ??? ??, ???? ??, ?? ??? ?? ??? ???? ?? ???????. ??? ???? ??? ??? ???? ??? ? ??? ?? ??? ?? ??? ??? ???? ???.

Django: ?????? ??? ??? ?? ??? ? ?? ??? ?????! Django? ????? ?? ??? ? ?????? ????????. MVC, MTV? ??? ??? ? ?? ??? ??? ? ??? ??? ? ??????? ?? ??? ? ????. Django? ??? ??? ??? ?? ??? ????? ?????? ??? ???? ??? ??? ?? ??? ? ??? ??? ? ????. Django? ????? ??? ??? ??? ??? ???? ????? ???? ????? ??? ??? ????.

Golang? ????? ??? ??: Golang? ????? ???? ?? ??? ??? ????? ???? ?? ??? ?????. ???? ??? ??????? ??? ???? ?? ????? ??? ?? ? ????? ????. ? ????? ??? ??? ????? ??? Golang? ??? ??? ? ? ????. ? ????? Golang? ????? ??? ??? ????? ???? ?? ?? ??? ?? ????? ????? ???? ?????. ????? ???? Golang? ??? ????? ???? ??? ?? ????.

Go ?????? Go? ?? ?????? ???? ?? ??? ??(?: ? ?? ? ?????? ??)? ???? ?? ?? ?????. ?? ???? Go ??????? Gin(? ??), GORM(?????? ??) ? RESTful(API ??)? ?????. ????? HTTP ?? ?? ??? ???? ????, ???? ???? ?? ???? ?? ?? ?? ??? ???? ? ?????. ?? ??? ??? ???? ???? ?? ??? ?????. gorilla/sessions? ???? ??? ??? ? ????.
