How to convert URL strings and query strings in nodejs _ node. js-js tutorial

Source: Internet
Author: User
This article describes how to convert URL strings and query strings in nodejs. For details, refer to the next complete URL string from "? "(Excluding ?) To "#" (if # exists) or end with the URL string (if # does not exist), this part is called a query string.

You can use the parse method in the Query String module to convert the String to an object. The usage of the parse method is as follows:

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

Str indicates the query string to be converted,

The Separator in the sep. String. The default value is &

Eq. The allocation character in the string. The default value is =. "=" the left side is the key, and the right side is the value.

Options: an object. You can use an integer maxKeys attribute to specify the number of attributes in the converted object. If you set the maxKeys attribute value to 0. the effect is equal to that without the maxKeys attribute value.

The Code is as follows:


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 converts a string into a query string format.

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

The Code is as follows:


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 a URL string into an object. Based on the different content in the URL string, the object may have the following attributes and their meanings.

Href: the original URL string to be converted.
Protocol: the protocol used by the client to send a request.
Slashes: use the "//" separator between the Protocol and path.
Host: the complete address and port number in the URL string. This address may be an IP address or a host name.
Auth: authentication information in the URL string.
Hostname: the complete address in the URL string. This address may be an IP address or a host name.
Search: the query string in the Url string, including the start character "? "
Path: the path in the url string, including the query string.
Query: the query string in the url string, excluding the start character "? ", Or the object converted based on the query string (the query attribute value is determined based on the parameters used by the parse () method );
Hash: hash string in the url string, including the starting character "#".

Url. parse (urlstr, [parseQueryString]);
UrlStr: the URL string to be converted,
ParseQueryString: A boolean value. When the parameter is set to true, the querystring module is used internally to convert the query string to an object. If the parameter value is set to false, this conversion operation is not performed. The default value is false.

The Code is as follows:


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

The Code is as follows:


{Protocol: 'http :',
Slashes: true,
Auth: 'user: pass ',
Host: 'host: 8080 ',
Port: '123 ',
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 Code is as follows:


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

The Code is as follows:


{Protocol: 'http :',
Slashes: true,
Auth: 'user: pass ',
Host: 'host: 8080 ',
Port: '123 ',
Hostname: 'host ',
Hash: '# name1 ',
Search :'? Username = sisi & age = 24 & sex = male ',
Query: {username: 'ssi', 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 difference between the first and second examples lies in the second parameter of parse, resulting in different query results.

You can convert an object converted from a url into a url string.

The Code is as follows:


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:

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

The above is all about converting the URL string and querying the string in node. It's actually quite simple to study.

Related Article

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.