码迷,mamicode.com
首页 > Web开发 > 详细

node中转换URL字符串与查询字符串

时间:2014-10-29 00:09:24      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   使用   for   

一个完整的URL字符串中,从"?"(不包括?)到"#"(如果存在#)或者到该URL字符串结束(如果不存在#)的这一部分称为查询字符串.

可以使用Query String模块中的parse方法将该字符串转换为一个对象,parse方法的使用方式如下所示:

querystring.parse(str,[sep],[eq],[options]);

str表示被转换的查询字符串,

sep.字符串中的分隔符,默认是&

eq.该字符串中的分配符,默认为=."="左边是key,右边是value

options:是一个对象,可以在该对象中使用一个整数值类型的maxKeys属性来指定转换后的对象中的属性个数,如果将maxKeys属性值设定为0.其效果等于不使用maxKeys属性值

 1 var querystring=require("querystring");
 2 var str="username=guoyansi&age=40&sex=male";
 3 var res=querystring.parse(str);
 4 console.log("1:%j",res);//1:{"username":"guoyansi","age":"40","sex":"male"}
 5 res=querystring.parse(str,"!");
 6 console.log("2:%j",res);//2:{"username":"guoyansi&age=40&sex=male"}
 7 res=querystring.parse(str,"&");
 8 console.log("3:%j",res);//3:{"username":"guoyansi","age":"40","sex":"male"}
 9 
10 str="username=guoyansi!age=40!sex=male";
11 res=querystring.parse(str,"!");
12 console.log("4:%j",res);//4:{"username":"guoyansi","age":"40","sex":"male"}
13 res=querystring.parse(str,"!","=");
14 console.log("5:%j",res);//5:{"username":"guoyansi","age":"40","sex":"male"}
15 res=querystring.parse(str,"!",":");
16 console.log("6:%j",res);//6:{"username=guoyansi":"","age=40":"","sex=male":""}
17 res=querystring.parse(str,"!","=",{maxKeys:2});
18 console.log("7:%j",res);//7:{"username":"guoyansi","age":"40"}

stringify是将字符串转化成查询字符串的格式.

querystring.stringify(obj,[sep],[eq])

1 var querystring=require("querystring");
2 var res= querystring.stringify({"username":"guoyansi","age":"40","sex":"male"});
3 console.log(res);//username=guoyansi&age=40&sex=male
4 res=querystring.stringify({"username":"guoyansi","age":"40","sex":"male"},"!");
5 console.log(res);//username=guoyansi!age=40!sex=male
6 res=querystring.stringify({"username":"guoyansi","age":"40","sex":"male"},"&",":");
7 console.log(res);//username:guoyansi&age:40&sex:male
8 res=querystring.stringify({"username":"guoyansi","age":["40","24"]},"&","=");
9 console.log(res);//username=guoyansi&age=40&age=24

在url模块中,可以使用parse()方法将URL字符串转换为一个对象,根据URL字符串中的不同内容,该对象可能具有的属性及其含义如下.

href:被转换的原URL字符串.

protocol:客户端发出请求时使用的协议.

slashes:在协议与路径中间时候使用"//"分隔符.

host:URL字符串中的完整地址及端口号,该地址可能为一个IP地址,也可能为一个主机名.

auth:URL字符串中的认证信息部分.

hostname:URL字符串中的完整地址,该地址可能为一个IP地址,也可能为一个主机名.

search:Url字符串中的查询字符串,包含起始字符"?"

path:url字符串中的路径,包含查询字符串.

query:url字符串中的查询字符串,不包含起始字符"?",或根据该查询字符串而转换的对象(根据parse()方法所用参数而决定query属性值);

hash:url字符串中的散列字符串,包含起始字符"#".

 

url.parse(urlstr,[parseQueryString]);

urlStr:是需要转换的URL字符串,

parseQueryString:是一个布尔值,当参数为true时,内部使用querystring模块查询字符串转换为一个对象,参数值为false时不执行该转换操作,默认是false

 

1 var url=require("url");
2 var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
3 var res=url.parse(str);
4 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‘ }

1 var url=require("url");
2 var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
3 var res=url.parse(str,true);
4 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的第二个参数,导致了结果中的query的不同

可以将一个url转换过的对象转换成一个url字符串.

1 var url=require("url");
2 var str="http://user:pass@host,com:8080/users/user.php?username=sisi&age=24&sex=male#name1";
3 var res=url.parse(str,true);
4 console.log(url.format(res));

结果是:

http://user:pass@host:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1

 

node中转换URL字符串与查询字符串

标签:style   blog   http   io   color   os   ar   使用   for   

原文地址:http://www.cnblogs.com/guoyansi19900907/p/4058262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!