一个完整的URL字符串中,从”?”(不包括?)到”#”(如果存在#)或者到该URL字符串结束(如果不存在#)的这一部分称为查询字符串.
可以使用Query String模块中的parse方法将该字符串转换为一个对象,parse方法的使用方式如下所示:
querystring.parse(str,[sep],[eq],[options]);
str表示被转换的查询字符串,
sep.字符串中的分隔符,默认是&
eq.该字符串中的分配符,默认为=.”=”左边是key,右边是value
options:是一个对象,可以在该对象中使用一个整数值类型的maxKeys属性来指定转换后的对象中的属性个数,如果将maxKeys属性值设定为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是将字符串转化成查询字符串的格式.
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”},”!”);









