Conversion of URL strings and query strings in nodejs

  • 2020-03-30 04:26:16
  • OfStack

A complete URL string from "?" (excluding ?) The part that goes to "#"(if there is #) or to the end of the URL string (if there is no #) is called the query string.

This String can be converted to an object using the parse method in the Query String module, which is used as follows:

The querystring. Parse (STR, [sep], [eq], [options]);

STR represents the converted query string,

The separator in the string sep. defaults to &

The allocator in the string eq. Defaults to =."=" with key on the left and value on the right

Options: is an object in which you can specify the number of properties in the converted object by using an integer value of the maxKeys property. If you set the maxKeys property value to 0, the effect is equal to not using the maxKeys property value


 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 is the format for converting a string to a query string.

The 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

In the url module, you can use the parse() method to convert the url string into an object that, depending on what is in the url string, may have the following properties and their meanings.

Href: the original URL string being converted.
Protocol: the protocol used when a client makes a request.
Slashes: use the "//" separator between the protocol and the path.
Host: the full address and port number in the URL string, which may be an IP address or a host name.
Auth: the authentication information part of the URL string.
Hostname: the full address in the URL string, which may be an IP address or a hostname.
Search: the query string in the Url string that contains the starting character "?"
Path: the path in the url string that contains the query string.
Query: the query string in the url string that does not contain the starting character "?" , or the object that is converted based on the query string (the query attribute value is determined based on the parameters used by the parse() method);
Hash: a hash string in the url string that contains the starting character "#".
 
Url. The parse (urlstr, [parseQueryString]);
UrlStr: is the URL string to be converted,
ParseQueryString: is a Boolean value. When the parameter is true, the querystring module will be used internally to convert the querystring into an object


 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' }

The first example differs from the second in that the second parameter of parse results in a different query in the result

A url-converted object can be converted to a url string.


 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));

The result is:

(link: http://user:pass@host:8080/, com/users/user. The PHP & # 63; The username = sisi&age = 24 & sex = male# name1)


Related articles: