nodejs中转换URL字符串与查询字符串详解
内容摘要
一个完整的URL字符串中,从"?"(不包括?)到"#"(如果存在#)或者到该URL字符串结束(如果不存在#)的这一部分称为查询字符串.
可以使用Query String模块中的parse方法将
可以使用Query String模块中的parse方法将
文章正文
一个完整的URL字符串中,从"?"(不包括?)到"#"(如果存在#)或者到该URL字符串结束(如果不存在#)的这一部分称为查询字符串.
可以使用Query String模块中的parse方法将该字符串转换为一个对象,parse方法的使用方式如下所示:
querystring.parse(str,[sep],[eq],[options]);
str表示被转换的查询字符串,
sep.字符串中的分隔符,默认是&
eq.该字符串中的分配符,默认为=."="左边是key,右边是value
options:是一个对象,可以在该对象中使用一个整数值类型的maxKeys属性来指定转换后的对象中的属性个数,如果将maxKeys属性值设定为0.其效果等于不使用maxKeys属性值
http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
var res=url.parse(str);
console.log(res);
var res=url.parse(str);
console.log(res);
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);
var res=url.parse(str,true);
console.log(res);
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));
var res=url.parse(str,true);
console.log(url.format(res));
结果是:
http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1
以上就是node中转换URL字符串与查询字符串的全部内容了,好好研究下,其实挺简单的。
代码注释