久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

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

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

      url模塊和querystring模塊是非常重要的兩個(gè)URL處理模塊。在做node服務(wù)端的開(kāi)發(fā)時(shí)會(huì)經(jīng)常用到。

      url

      在介紹url模塊之前我們先來(lái)一張圖,看懂了這張圖對(duì)于url這個(gè)模塊你就基本上沒(méi)什么問(wèn)題了。

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

      我們來(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);
      登錄后復(fù)制

      輸出

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

      該方法還支持傳遞另外兩個(gè)參數(shù),parseQueryStringslashesDenoteHos

      parseQueryString:(默認(rèn)為false)如為false,則urlObject.query為未解析的字符串,比如nick=%E4%B8%AD%E6%96%87,且對(duì)應(yīng)的值不會(huì)decode;如果parseQueryString為true,則urlObject.queryobject,比如{ 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);
      登錄后復(fù)制

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

      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);
      登錄后復(fù)制

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

      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
      登錄后復(fù)制

      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
      登錄后復(fù)制

      querystring

      querystring這個(gè)模塊,也是用來(lái)做url查詢參數(shù)的解析。這里我們重點(diǎn)分析下它的parsestringify兩個(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: '中文' }
      登錄后復(fù)制

      下面我們?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' }
      登錄后復(fù)制

      相當(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
      登錄后復(fù)制

      這個(gè)方法也是支持自定義分割符的。

      const obj5 = {   name: "randy",   country: "cn", }; const str6 = querystring.stringify(obj5, "|", "-"); console.log(str6); // name-randy|country-c
      登錄后復(fù)制

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)