nodejs中轉(zhuǎn)換URL字符串與查詢字符串詳解
一個完整的URL字符串中,從"?"(不包括?)到"#"(如果存在#)或者到該URL字符串結(jié)束(如果不存在#)的這一部分稱為查詢字符串.
可以使用Query String模塊中的parse方法將該字符串轉(zhuǎn)換為一個對象,parse方法的使用方式如下所示:
querystring.parse(str,[sep],[eq],[options]);
str表示被轉(zhuǎn)換的查詢字符串,
sep.字符串中的分隔符,默認是&
eq.該字符串中的分配符,默認為=."="左邊是key,右邊是value
options:是一個對象,可以在該對象中使用一個整數(shù)值類型的maxKeys屬性來指定轉(zhuǎn)換后的對象中的屬性個數(shù),如果將maxKeys屬性值設(shè)定為0.其效果等于不使用maxKeys屬性值
var querystring=require("querystring");
var str="username=guoyansi&age=40&sex=male";
var res=querystring.parse(str);
console.log("1:%j",res);//1:{"username":"guoyansi","age":"40","sex":"male"}
res=querystring.parse(str,"!");
console.log("2:%j",res);//2:{"username":"guoyansi&age=40&sex=male"}
res=querystring.parse(str,"&");
console.log("3:%j",res);//3:{"username":"guoyansi","age":"40","sex":"male"}
str="username=guoyansi!age=40!sex=male";
res=querystring.parse(str,"!");
console.log("4:%j",res);//4:{"username":"guoyansi","age":"40","sex":"male"}
res=querystring.parse(str,"!","=");
console.log("5:%j",res);//5:{"username":"guoyansi","age":"40","sex":"male"}
res=querystring.parse(str,"!",":");
console.log("6:%j",res);//6:{"username=guoyansi":"","age=40":"","sex=male":""}
res=querystring.parse(str,"!","=",{maxKeys:2});
console.log("7:%j",res);//7:{"username":"guoyansi","age":"40"}
stringify是將字符串轉(zhuǎn)化成查詢字符串的格式.
querystring.stringify(obj,[sep],[eq])
var querystring=require("querystring");
var res= querystring.stringify({"username":"guoyansi","age":"40","sex":"male"});
console.log(res);//username=guoyansi&age=40&sex=male
res=querystring.stringify({"username":"guoyansi","age":"40","sex":"male"},"!");
console.log(res);//username=guoyansi!age=40!sex=male
res=querystring.stringify({"username":"guoyansi","age":"40","sex":"male"},"&",":");
console.log(res);//username:guoyansi&age:40&sex:male
res=querystring.stringify({"username":"guoyansi","age":["40","24"]},"&","=");
console.log(res);//username=guoyansi&age=40&age=24
在url模塊中,可以使用parse()方法將URL字符串轉(zhuǎn)換為一個對象,根據(jù)URL字符串中的不同內(nèi)容,該對象可能具有的屬性及其含義如下.
href:被轉(zhuǎn)換的原URL字符串.
protocol:客戶端發(fā)出請求時使用的協(xié)議.
slashes:在協(xié)議與路徑中間時候使用"http://"分隔符.
host:URL字符串中的完整地址及端口號,該地址可能為一個IP地址,也可能為一個主機名.
auth:URL字符串中的認證信息部分.
hostname:URL字符串中的完整地址,該地址可能為一個IP地址,也可能為一個主機名.
search:Url字符串中的查詢字符串,包含起始字符"?"
path:url字符串中的路徑,包含查詢字符串.
query:url字符串中的查詢字符串,不包含起始字符"?",或根據(jù)該查詢字符串而轉(zhuǎn)換的對象(根據(jù)parse()方法所用參數(shù)而決定query屬性值);
hash:url字符串中的散列字符串,包含起始字符"#".
url.parse(urlstr,[parseQueryString]);
urlStr:是需要轉(zhuǎn)換的URL字符串,
parseQueryString:是一個布爾值,當參數(shù)為true時,內(nèi)部使用querystring模塊查詢字符串轉(zhuǎn)換為一個對象,參數(shù)值為false時不執(zhí)行該轉(zhuǎn)換操作,默認是false
var url=require("url");
var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
var res=url.parse(str);
console.log(res);
{ protocol: 'http:',
slashes: true,
auth: 'user:pass',
host: 'host:8080',
port: '8080',
hostname: 'host',
hash: '#name1',
search: '?username=sisi&age=24&sex=male',
query: 'username=sisi&age=24&sex=male',
pathname: '/,com/users/user.php',
path: '/,com/users/user.php?username=sisi&age=24&sex=male',
href: 'http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1' }
var url=require("url");
var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
var res=url.parse(str,true);
console.log(res);
{ protocol: 'http:',
slashes: true,
auth: 'user:pass',
host: 'host:8080',
port: '8080',
hostname: 'host',
hash: '#name1',
search: '?username=sisi&age=24&sex=male',
query: { username: 'sisi', age: '24', sex: 'male' },
pathname: '/,com/users/user.php',
path: '/,com/users/user.php?username=sisi&age=24&sex=male',
href: 'http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1' }
第一個例子和第二個例子不同之處在于parse的第二個參數(shù),導(dǎo)致了結(jié)果中的query的不同
可以將一個url轉(zhuǎn)換過的對象轉(zhuǎn)換成一個url字符串.
var url=require("url");
var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
var res=url.parse(str,true);
console.log(url.format(res));
結(jié)果是:
http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1
以上就是node中轉(zhuǎn)換URL字符串與查詢字符串的全部內(nèi)容了,好好研究下,其實挺簡單的。
相關(guān)文章
window10下node使用管理神器NVM安裝配置超詳細步驟
nvm全稱Node Version Manager是 Nodejs 版本管理器,它讓我們能方便的對 Nodejs 的版本進行切換,nvm 的官方版本只支持 Linux 和 Mac, Windows 用戶,可以用 nvm-windows,這篇文章主要介紹了window10下node使用管理神器NVM安裝配置超詳細步驟,需要的朋友可以參考下2023-01-01Node.JS更改Windows注冊表Regedit的方法小結(jié)
注冊表是windows操作系統(tǒng)中的一個核心數(shù)據(jù)庫,這里介紹一些通過node.js操作注冊表的幾種方法,感興趣的朋友參考下吧2017-08-08使用Nodejs?實現(xiàn)一個簡單的?Redis客戶端(推薦)
在nodejs中支持TCP連接的是net模塊,?其中使用createConnection(config)或者直接new?Socket(config)來初始化一個TCP連接,這篇文章主要介紹了用Nodejs?實現(xiàn)一個簡單的?Redis客戶端,需要的朋友可以參考下2022-11-11深入理解Node.js 事件循環(huán)和回調(diào)函數(shù)
這篇文章主要介紹了深入理解Node.js 事件循環(huán)和回調(diào)函數(shù),詳細的介紹Node.js 事件循環(huán)和Node.js回調(diào)函數(shù),需要學(xué)習的可以參考一下。2016-11-11Node.js在child_process域和錯誤冒泡及捕獲實踐
這篇文章主要為大家介紹了Node.js在child_process域和錯誤冒泡及捕獲實踐示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11