nodejs中轉(zhuǎn)換URL字符串與查詢字符串詳解
一個完整的URL字符串中,從"?"(不包括?)到"#"(如果存在#)或者到該URL字符串結(jié)束(如果不存在#)的這一部分稱為查詢字符串.
可以使用Query String模塊中的parse方法將該字符串轉(zhuǎn)換為一個對象,parse方法的使用方式如下所示:
querystring.parse(str,[sep],[eq],[options]);
str表示被轉(zhuǎn)換的查詢字符串,
sep.字符串中的分隔符,默認(rèn)是&
eq.該字符串中的分配符,默認(rèn)為=."="左邊是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地址,也可能為一個主機(jī)名.
auth:URL字符串中的認(rèn)證信息部分.
hostname:URL字符串中的完整地址,該地址可能為一個IP地址,也可能為一個主機(jī)名.
search:Url字符串中的查詢字符串,包含起始字符"?"
path:url字符串中的路徑,包含查詢字符串.
query:url字符串中的查詢字符串,不包含起始字符"?",或根據(jù)該查詢字符串而轉(zhuǎn)換的對象(根據(jù)parse()方法所用參數(shù)而決定query屬性值);
hash:url字符串中的散列字符串,包含起始字符"#".
url.parse(urlstr,[parseQueryString]);
urlStr:是需要轉(zhuǎn)換的URL字符串,
parseQueryString:是一個布爾值,當(dāng)參數(shù)為true時,內(nèi)部使用querystring模塊查詢字符串轉(zhuǎn)換為一個對象,參數(shù)值為false時不執(zhí)行該轉(zhuǎn)換操作,默認(rè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)容了,好好研究下,其實(shí)挺簡單的。
相關(guān)文章
koa2 數(shù)據(jù)api中間件設(shè)計模型的實(shí)現(xiàn)方法
這篇文章主要介紹了koa2 數(shù)據(jù)api中間件設(shè)計模型的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Node.js 使用遞歸實(shí)現(xiàn)遍歷文件夾中所有文件
這篇文章主要介紹了Node.js使用遞歸實(shí)現(xiàn)遍歷文件夾中所有文件,需要的朋友可以參考下2017-09-09
Node.js 8 中的 util.promisify的詳解
本篇文章主要介紹了Node.js 8 中的 util.promisify的詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
node如何將package.json中的包降為低版本或者升級為高版本
比如現(xiàn)在你用某個包的當(dāng)前版本,但是你安裝的版本高了,那么你應(yīng)該這么做,首先刪除node項目中的node_modules目錄,防止安裝時的包不一致,下面給大家介紹node將package.json中的包降為低版本或者升級為高版本的方法,感興趣的朋友一起看看吧2023-11-11
ajax+node+request爬取網(wǎng)絡(luò)圖片的實(shí)例(宅男福利)
下面小編就為大家?guī)硪黄猘jax+node+request爬取網(wǎng)絡(luò)圖片的實(shí)例(宅男福利)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

