首頁  >  文章  >  web前端  >  聊聊Node中的url模組和querystring模組

聊聊Node中的url模組和querystring模組

青灯夜游
青灯夜游轉載
2023-02-23 19:39:251897瀏覽

聊聊Node中的url模組和querystring模組

url模組和querystring模組是非常重要的兩個URL處理模組。在做node服務端的開發時會常用到。

url

在介紹url模組之前我們先來一張圖,看懂了這張圖對於url這個模組你就基本上沒什麼問題了。

聊聊Node中的url模組和querystring模組

我們來解釋下各自的意義

  • protocol:協議,需要注意的是包含了:,並且是小寫的。 【相關教學推薦:nodejs影片教學程式設計教學
  • slashes:如果:後面跟了兩個// ,那麼為true。
  • auth:認證訊息,如果有密碼,為usrname:passwd,如果沒有,則為usrname。注意,這裡區分大小寫。
  • host:主機名稱。注意包含了端口,例如ke.qq.com:8080,並且是小寫的。
  • hostname:主機名,不包含端口,並且是小寫的。
  • port: 埠號。
  • path:路徑部分,包含search部分。
  • pathname:路徑部分,不包含search部分。
  • search:查詢字串,注意,包含了?,此外,值是沒有經過decode的。
  • query:字串 或 物件。如果是字串,則是search去掉?,其餘一樣;如果是對象,那麼是decode過的。
  • hash:哈希部分,注意包含了#。
  • href:原始的位址。不過要注意的是,protocolhost會被轉成小寫字母。

#下面我們來講解下它的三個常用方法

parse(urlString, parseQueryString, slashesDenoteHost)

#該方法將會url字串,解析成object,方便開發者進行操作。

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);

輸出

聊聊Node中的url模組和querystring模組

該方法也支援傳遞另外兩個參數,parseQueryStringslashesDenoteHos

parseQueryString:(預設為false)如為false,則urlObject.query為未解析的字串,例如nick=中文,且對應的值不會decode;如果parseQueryString為true,則urlObject.queryobject

{ nick: '中文' }

,且值會被`decode;聊聊Node中的url模組和querystring模組

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

的一部份。

光看起來可能不太懂這句話的意思,下面筆者舉個例子我相信你們就明白了。 聊聊Node中的url模組和querystring模組

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查詢參數的解析。這裡我們重點分析下它的parse

stringify

兩個方法。

parse(str, sep, eq, options)

#parse是將查詢字串轉換成物件類型,也會

decode

<pre class="brush:js;toolbar:false;">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: '中文' }</pre>下面我們再來看看它的第二個和第三個參數。其實相當於可以替換

&、=

為自訂字符,下面筆者舉個例子就很快明白了。 <pre class="brush:js;toolbar:false;">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' }</pre>相當於把&替換成|,把=取代了

-

。筆者覺得配到這種情況應該不多。

stringify(obj, sep, eq, options)

這個方法就是上面

parse

的反向運算。下面咱們直接上例子

const obj3 = {
  nick: "randy",
  age: "24",
};
const str4 = querystring.stringify(obj3);
console.log(str4); // nick=randy&age=24
###這個方法也是支援自訂分割符的。 ###
const obj5 = {
  name: "randy",
  country: "cn",
};
const str6 = querystring.stringify(obj5, "|", "-");
console.log(str6); // name-randy|country-c

更多node相关知识,请访问:nodejs 教程

以上是聊聊Node中的url模組和querystring模組的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:juejin.cn。如有侵權,請聯絡admin@php.cn刪除