Convert URL string to query string in node

Source: Internet
Author: User

A complete URL string, from the "?" (not included?) To "#" (If there is #) or to the end of the URL string (if no #) is called the query string.

You can use the parse method in the query string module to convert the string to an object, using the Parse method as follows:

Querystring.parse (Str,[sep],[eq],[options]);

STR represents the query string being converted,

Sep. Delimiter in string, default is &

Eq. The identifier in the string, which defaults to =. " = "Left is key, right is value

Options: is an object in which you can specify the number of properties in the converted object using an integer value type of Maxkeys property, if the Maxkeys property value is set to 0. The effect is equal to not using the Maxkeys property value

1 varQuerystring=require ("QueryString");2 varStr= "Username=guoyansi&age=40&sex=male";3 varres=querystring.parse (str);4Console.log ("1:%j", res);//1:{"username": "Guoyansi", "Age": "Max", "Sex": "Male"}5Res=querystring.parse (str, "!");6Console.log ("2:%j", res);//2:{"username": "Guoyansi&age=40&sex=male"}7Res=querystring.parse (str, "&");8Console.log ("3:%j", res);//3:{"username": "Guoyansi", "Age": "Max", "Sex": "Male"}9 TenStr= "Username=guoyansi!age=40!sex=male"; OneRes=querystring.parse (str, "!"); AConsole.log ("4:%j", res);//4:{"username": "Guoyansi", "Age": "Max", "Sex": "Male"} -Res=querystring.parse (str, "!", "="); -Console.log ("5:%j", res);//5:{"username": "Guoyansi", "Age": "Max", "Sex": "Male"} theRes=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": "Max"}

Stringify is the format of converting a string into a query string.

Querystring.stringify (Obj,[sep],[eq])

1 varQuerystring=require ("QueryString");2 varRes= querystring.stringify ({"username": "Guoyansi", "Age": "Max", "Sex": "Male"});3Console.log (RES);//Username=guoyansi&age=40&sex=male4Res=querystring.stringify ({"username": "Guoyansi", "Age": "Max", "Sex": "Male"}, "!");5Console.log (RES);//Username=guoyansi!age=40!sex=male6Res=querystring.stringify ({"username": "Guoyansi", "Age": "Max", "Sex": "Male"}, "&", ":");7Console.log (RES);//Username:guoyansi&age:40&sex:male8Res=querystring.stringify ({"username": "Guoyansi", "Age": ["+", "]"}, "&", "=");9Console.log (RES);//username=guoyansi&age=40&age=24

In a URL module, you can use the parse () method to convert a URL string to an object, depending on the different content in the URL string, the properties that the object might have and its meaning as follows.

HREF: The original URL string that was converted.

Protocol: The protocol that the client uses when making the request.

Slashes: Use the "//" delimiter in the middle of the protocol and path.

Host:url the full address and port number in the string, which may be an IP address or a host name.

Auth:url The authentication Information section in the string.

Hostname:url the full address in the string, which may be an IP address or a host name.

Search:url the query string in the string, containing the starting character "?"

The path in the Path:url string that contains the query string.

Query:url a query string in a string that does not contain the starting character "?", or an object that is converted based on the query string (depending on the parameter used by the parse () method, the query property value);

Hash:url the hash string in the string, containing the starting character "#".

Url.parse (urlstr,[parsequerystring]);

URLSTR: Is the URL string that needs to be converted,

Parsequerystring: Is a Boolean value that, when the argument is true, is internally converted to an object using the QueryString module query string, and the parameter value is false when the conversion operation is not performed, the default is False

1 var url=require ("url"); 2 var str= "Http://user:[email protected],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:[email  protected]:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1 '}

1 var url=require ("url"); 2 var str= "Http://user:[email protected],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: ' $ ', Sex: ' Male '},
Pathname: '/,com/users/user.php ',
Path: '/,com/users/user.php?username=sisi&age=24&sex=male ',
HREF: ' Http://user:[email protected]:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1 '}

The first and second examples differ in the second argument of parse, which results in a different query in the result

You can convert a URL-converted object into a URL string.

1 var url=require ("url"); 2 var str= "Http://user:[email protected],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));

The result is:

Http://user:[email protected]:8080/,com/users/user.php?username=sisi&age=24&sex=male#name1

Convert URL string to query string in node

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.