nodejs中转换URL字符串与查询字符串详解

  一个完整的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"},"!");

  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字符串转换为一个对象,根据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

  

复制代码 代码如下:

  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的第二个参数,导致了结果中的query的不同

  可以将一个url转换过的对象转换成一个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));

  结果是:

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

  以上就是node中转换URL字符串与查询字符串的全部内容了,好好研究下,其实挺简单的。