zoukankan      html  css  js  c++  java
  • node中转换URL字符串与查询字符串

    一个完整的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

  • 相关阅读:
    在Js或者cess后加版本号 防止浏览器缓存
    svn操作
    Hash表
    网站js埋点
    c#优秀文章
    CentOS修改默认yum源为国内yum镜像源
    mysql开启远程连接
    安装jdk环境
    Eclipse的一下设置
    好用的在线HTML、CSS工具
  • 原文地址:https://www.cnblogs.com/guoyansi19900907/p/4058262.html
Copyright © 2011-2022 走看看