How to use querystring module in node. js introductory tutorial

  • 2021-07-24 09:31:11
  • OfStack

querystring module

querystring literally means a query string, and 1 is generally used to parse the data that comes with an http request. The querystring module provides only four methods, which, in my opinion, correspond to each other.

These four methods are querystring.parse And querystring.stringify , querystring.escape And querystring.unescape .

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


const querystring = require("querystring");

Secondly, you can use the method under the module:

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

parse This method deserializes a string to an object.

Parameters:

str refers to the string to be deserialized;

separator (optional) refers to the character or string used to divide the string str. The default value is " & ";

eq (optional) refers to the character or string used to divide the key from the value, with a default value of "=";

options (optional) This parameter is an object in which the maxKeys and decodeURIComponent properties can be set:

1. maxKeys: Pass in 1 number type, specify the maximum value of parsing key-value pair, and the default value is 1000. If it is set to 0, the number limit of parsing will be cancelled;

2. decodeURIComponent: Pass in 1 function to decode a string containing%, with a default value of querystring.unescape . In the official API example, using gbkDecodeURIComponent will report an error, showing gbkDecodeURIComponent is no defined, because it needs to be defined before using this gbkDecodeURIComponent method. Assuming gbkDecodeURIComponent function already exists is also written in API... This sentence means "suppose this 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 serializes an object into a string, which is the same as the querystring.parse Relatively.

Parameters:

obj refers to the object that needs to be serialized

separator (optional) is a character or string used to concatenate key-value pairs, with a default value of " & ";

eq (optional) characters or strings used to concatenate keys and values, with a default value of "=";

options (save) passes in 1 object that sets the encodeURIComponent property:

1. encodeURIComponent: The value is of type function and converts an insecure url string to a percentage. The default value is function 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 Encodes Incoming Strings

Example 3, querystring. escape


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

4. querystring.unescape(str)

The unescape method decodes a string containing%

Example 4, querystring. unescape


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

Summarize

querystring is relatively simple, with only four methods.

1. querystring.stringify Serialization;

2. querystring.parse Deserialization;

3. querystring.escape Coding;

4. querystring.unescape Decoding;

Well, the above is the whole content of this article. Of course, my research on this module is still not deep. I only made a simple translation of API of this module and added my own understanding. If there are mistakes, I hope to correct them and discuss them. At the same time, I hope the content of this paper can be of great help to everyone.


Related articles: