The querystring module of nodejs

Here we mainly write down the usage of the querystring module.

Querystring literally means a query string, which generally parses the data carried by the HTTP request . The querystring module only provides 4 methods, which in my opinion correspond to each other.

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

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

1 const querystring = require("querystring");

Second, you can use the methods under the module:

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

The parse method is to deserialize a string into an object.

Parameters: str refers to the string to be deserialized;

   separator (optional) refers to the character or string used to separate the string of str, the default value is "&";

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

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

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

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

Example 1, querystring.parse

copy code
 1 querystring.parse("name=whitemu&sex=man&sex=women");
 2 /*
 3 return:
 4 { name: 'whitemu', sex: [ 'man', 'women' ] }
 5 */
 6 querystring.parse("name=whitemu#sex=man#sex=women","#",null,{maxKeys:2});
 7 /*
 8 return:
 9 { name: 'whitemu', sex: 'man' }
10 */
copy code

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

The stringify method is to serialize an object into a string, as opposed to querystring.parse.

Parameters: obj refers to the object to be serialized

   separator (optional) character or string used to connect key-value pairs, the default value is "&";

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

   options(可省)传入一个对象,该对象可设置encodeURIComponent这个属性:

      encodeURIComponent:值的类型为function,可以将一个不安全的url字符串转换成百分比的形式,默认值为querystring.escape()。

例子2,querystring.stringify

copy code
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'
*/
copy code

querystring.escape(str)

escape可使传入的字符串进行编码

例子3,querystring.escape

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

querystring.unescape(str)

unescape方法可将含有%的字符串进行解码

Example 4, querystring.unescape

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

Summarize:

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

  querystring.stringify serialize;

  querystring.parse deserialize;

  querystring.escape encoding;

  querystring.unescape decode;

  Of course, my research on this module is still not deep, and I just made a simple translation of the API of this module and added some understandings of my own. If there are any mistakes, I hope to correct them and discuss together. . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326297334&siteId=291194637