Home  >  Article  >  Web Front-end  >  Detailed explanation of how to use querystring module example code in node.js

Detailed explanation of how to use querystring module example code in node.js

伊谢尔伦
伊谢尔伦Original
2017-07-24 11:11:341403browse

querystring literally means query string, which generally parses the data carried by http requests. The querystring module only provides 4 methods, and these 4 methods are corresponding.

The four methods are querystring.parse and querystring.stringify, querystring.escape and querystring.unescape.

First of all, before using the querystring module, you need to require it:


const querystring = require("querystring");

Secondly, 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.

Parameters:

str refers to the string that needs to be deserialized;

separator (optional) refers to the string used to split str Character or string, the default value is "&";

 eq (optional) refers to the character or string used to divide the key and value, the default value is "=";

 options (can be omitted) This parameter is an object in which the two attributes maxKeys and decodeURIComponent can be set:

1.maxKeys: Pass in a number type to specify the maximum value of the parsed key-value pair. The default value is 1000 , if set to 0, the limit on the number of parses will be cancelled;

2.decodeURIComponent: Pass in a function to decode strings containing %, the default value is querystring.unescape. In the official API example, when using the gbkDecodeURIComponent method, an error will be reported, showing that gbkDecodeURIComponent is no defined. This is because it needs to be defined before using the gbkDecodeURIComponent method. In the API, it is also written Assuming gbkDecodeURIComponent function already exists... This sentence means "assuming that the gbkDecodeURIComponent method already exists".

Example 1, querystring.parse


querystring.parse("name=whitemu&sex=man&sex=women");
/*
return:
{ name: 'whitemu', sex: [ 'man', 'women' ] }
*/
querystring.parse("name=whitemu#sex=man#sex=women","#",null,{maxKeys:2});
/*
return:
{ name: 'whitemu', sex: 'man' }
*/

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

stringify This method is to serialize an object into a string, which is opposite to querystring.parse.

Parameters:

Obj refers to the object that needs to be serialized

Separator (optional) is a character or string used to connect key-value pairs. The default value is "&";

 eq (can be omitted) The character or string used to connect the key and value, the default value is "=";

  options (can be omitted) is passed in An object that can set the encodeURIComponent attribute:

1. encodeURIComponent: The value type is function, which can convert an unsafe url string into a percentage form. The default value is querystring. escape() .

Example 2, querystring.stringify


querystring.stringify({name: 'whitemu', sex: [ 'man', 'women' ] });
/*
return:
'name=whitemu&sex=man&sex=women'
*/
querystring.stringify({name: 'whitemu', sex: [ 'man', 'women' ] },"*","$");
/*
return:
'name$whitemu*sex$man*sex$women'
*/

3. querystring.escape(str)

escape can encode the incoming string

Example 3, querystring.escape


querystring.escape("name=慕白");
/*
return:
'name%3D%E6%85%95%E7%99%BD'
*/

4. querystring.unescape(str)

unescape method can decode strings containing %

Example 4, querystring.unescape


querystring.unescape('name%3D%E6%85%95%E7%99%BD');
/*
return:
'name=慕白'
*/

querystring This module is relatively simple, with only 4 methods.

1. querystring.stringifySerialization;

2.querystring.parseDeserialization;

3. querystring.escapeencoding;

4.querystring.unescapedecoding;

The above is the detailed content of Detailed explanation of how to use querystring module example code in node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn