Nodejs's QueryString Module

Source: Internet
Author: User

The main note here is how to use the QueryString module.

The literal meaning of QueryString is the query string, which is typically parsed with the data that the HTTP request takes. The QueryString module only provides 4 methods, and in my opinion, these 4 methods are relative to each other.

These 4 methods are Querystring.parse and Querystring.stringify,querystring.escape and Querystring.unescape respectively.

First, you need to require in before using the QueryString module:

1 Const querystring = require ("querystring");

Second, you can use the method under the module:

1 querystring.parse (str,separator,eq,options)

Parse this method is to deserialize a string into an object.

Parameter: str refers to a string that needs to be deserialized;

Separator (can be saved) refers to the character or string used to split the STR string, and the default value is "&";

EQ (can be saved) refers to the character or string used to divide the key and value, and the default value is "=";

Options (can be saved) This parameter is an object that can set both Maxkeys and decodeURIComponent properties:

Maxkeys: A number type is passed in, specifying the maximum value of the resolved key-value pair, the default value is 1000, and if set to 0 o'clock, the resolution limit is canceled;

decodeURIComponent: A function is passed in to decode the string containing%, the default value is Querystring.unescape. In the official API example, using Gbkdecodeuricomponent This method will error, showing gbkdecodeuricomponent is no defined, This is because the Gbkdecodeuricomponent method needs to be defined before it can be used. In the API also wrote the assuming gbkdecodeuricomponent function already exists ... This sentence means "Suppose this gbkdecodeuricomponent method already exists".

Example 1,querystring.parse

1Querystring.parse ("Name=whitemu&sex=man&sex=women");2 /*3 return:4 {name: ' Whitemu ', sex: [' man ', ' Women ']}5 */6Querystring.parse ("Name=whitemu#sex=man#sex=women", "#",NULL, {Maxkeys:2});7 /*8 return:9 {name: ' Whitemu ', Sex: ' Man '}Ten */

2 querystring.stringify (obj,separator,eq,options)

Stringify This method is to serialize an object into a string, as opposed to querystring.parse.

Parameter: obj refers to the object that needs to be serialized

Separator (can be saved) the character or string used to connect key-value pairs, with the default value of "&";

EQ (can save) the character or string that is used to connect the key and value, and the default value is "=";

The options (which can be saved) pass in an object that sets the encodeURIComponent property:

encodeURIComponent: The type of the value is function, you can convert an unsafe URL string to a percentage, the default value is Querystring.escape ().

Example 2,querystring.stringify

Querystring.stringify ({name: ' Whitemu ', sex: [' man ', ' Women ' ]}); /* return: ' Name=whitemu&sex=man&sex=women ' */  ' Whitemu ', sex: [' man ', ' Women ']}, ' * ', ' $ '); /* return: ' Name$whitemu*sex$man*sex$women ' */

3 querystring.escape (str)

Escape to encode an incoming string

Example 3,querystring.escape

Querystring.escape ("name= mu bai"); /* return: ' NAME%3D%E6%85%95%E7%99%BD ' */

4 querystring.unescape (str)

Unescape method to decode a string containing%

Example 4,querystring.unescape

Querystring.unescape (' name%3d%e6%85%95%e7%99%bd '); /* return: ' name= mu Bai ' */

Summarize:

QueryString This module is relatively simple, there are only 4 methods.

serialization of Querystring.stringify;

Querystring.parse deserialization;

Querystring.escape coding;

Querystring.unescape decoding;

Of course, my humble study of the module is still not deep, just the API of the module to do a simple translation and add some of their own understanding, if there is a mistake hope to correct, discuss together ...

Nodejs's QueryString Module

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.