Node.js Gets the use of QueryString modules for GET, POST data

Whether it is the front end or the backend, the application scenario that often appears is the processing of parameters in the URL. The QueryString module of NodeJS provides some tools that process Query Strings. This article will detail the querystring

 var queryString = Require ('queryString'); / * {Unescape, Unescape: [Function: Qsunescape], Escape: [Function], Encode: [Function], Stringify: [Function], Decode: [Function], PARSE: [Function]} * / console.log (queryString);   
Serialization

queryString.Parse (Str [, SEP [, EQ [, Options]])

QueryString.Parse () method can put a URL string ( STR) parsing a set of key value pairs, parameters are as follows

STR

The URL query string to be parsed. SEP

A sub-string for defining the key value pair in the query string. The default is ‘&’.

EQ Used to define a sub-string of the key and value in the query string. Default is ‘=’.

Options

DecodeURMPONENT

When the function of the query string is used, the function is used when the band-encoded character is decoded. Default is querystring.unescape ()

MaxKeys

Specifies the maximum number of keys to be parsed. The default is 1000. Specifies to 0 limit limit

VAR queryString = Require (‘queryString’); var str = ‘foo = bar & abc = xyz & abc = 123’ Console.log (queryString.Parse (STR)); // ‘{foo:’ bar ‘, ABC: [‘ XYZ ‘,’ 123 ‘]}’
  The second parameter is used to define a sub-string of key value pairs in the query string  
var queryString = Require (‘queryString’); Var str = ‘foo = BAR & ABC = XYZ & ABC = 123 ‘; console.log (queryString.Parse (STR,’ A ‘)); // {foo:’ b ‘,’ R & ‘:’, BC: [‘XYZ &’, ‘123’

The third parameter is used to define a sub-string
   

   var queryString = Require ('queryString'); var str = 'foo = bar & abc = xyz & abc = 123'; console.log (queryString.Parse (Str, '&', 'c')); // {'foo = Bar ':' ', AB: [' = XYZ ',' = 123 ']} 
[Note] QueryString.Parse () method returnedObjects do not inherit Object from JavaScript. This means that the typical Object method is like obj.tostring (), Obj.hasownProperty (), etc. is not defined and cannot be used by

By default, the percentage-encoded characters in the query string will be considered UTF-8 encoding is used. If another character encoding is used, the decodeuricomponent option needs to be specified

var queryString = Require (‘queryString’); // {w: ‘ ‘, Foo:’ bar ‘} console.log (querystring.parse (‘ w =% D6% D0% CE% C4 & foo = bar ‘, null, null, {decodeuricomponent:’ gbkdecodeuricomponent ‘});
   QueryString.Stringify (Obj [, SEP] [, EQ] [, Options]) 
QueryString.Stringify () method is the reverse direction of querystring.parse () method Operation, by traversing the own properties of the object, generating a URL query string from a given OBJ, the parameters are as follows

OBJ

To serialize the object of a URL query string

SEP

A sub-string for defining the key value pair in the query string. The default is ‘&’

EQ

The sub-string used to define the keys and values ​​in the query string. Default is ‘=’

optionuRicomponent

Converts a character to URL unsafe?The function used when the percentage coding in the string is inquired. Default is querystring.escape ()

var queryString = Require (‘queryString’); // ‘foo = bar & baz = quix & baz = quux & corge =’ console.log (QueryString.Stringify ({foo: ‘Bar’, Baz: [‘QUX’, ‘Quux’], Corge: ”}); var queryString = Require (‘querystring’); // ‘foo: bar; baz : qux’console.log (querystring.stringify ({foo: ‘bar’, baz: ‘qux’}, ‘;’, ‘:’));

QueryString Module Encoding
QueryString.escape (STR)
  QueryString.escape () method executes URL percent code for a given STR, like Encodeuricomponent method  
querystring .scape () method is used for querystring.stringify () and is usually not used directly. It is open to the outside, to rewrite the code to rewrite the code by assigning a function to QueryString.escape when needed
VAR queryString = Require (‘queryString’) Console.log (ENCODEURMPONENT (‘test’)); //% E6% B5% 8B% E8% AF% 95console.log (Qu)ERYYSTRING.ESCAPE (‘test’)); //% E6% B5% 8B% E8% AF% 95

queryString.unescape (str)

queryString.unescape () method performs decoding

queryString.unescape () method for querystring.parse (), is not directly used by querystring.parse (). use. It is open to the outside, which can rewrite the decoding implementation by assigning a function to QueryString.uneScape to assign a function when needed.

QueryString.unescape () method defaults to decode

  VAR QueryString = Require ('queryString'); LOG (decodeuricomponent ('% E6% B5% 8B% E8% AF% 95')); // 'Test' Console.log (QueryString.unescape ('% E6% B5% 8B% E8% AF% 95'))) ; // 'Test'  
Node.js Gets the data
GET request data is stored in the URL

http://127.0.0.1:8080/Home/test?a=1&b=2var http = Require (‘http’); var URL = Require (‘url’); var queryString = Require (‘ QueryString ‘); http.cREATSERVER (REQ, RES) {var Urlobj = url.parse (reg.URL); var query = urlobj.query; var queryobj = queryString.Parse (query); console.log (Req.url); // ‘ / home / test? a = 1 & b = 2 ‘console.log (query); //’ a = 1 & b = 2 ‘console.log (queryobj); // {a:’ 1 ‘, b:’ 2 ‘}} ) .Listen (8080);

Node.js Gets the POST data

The data requested by the POST request will be written into the buffer, which needs to pass through Request Data event and END events to perform data splicing processing
   VAR http = required ('http'); VAR URL = Require ('url'); var queryString = Require 'querystring'); http.createserver (Function (REQ, RES) {var str = '; Req.on (' Data ', Function (THUNK) {STR + = Thunk;}; Req.on (' end " , function () {console.log (STR); // 'name = a & email = b% 40b.com' var queryobj = queryString.Parse (str); console.log (queryobj); // {name: 'a' , email: 'b% 40b.com'}}); 
More about node.js to get GET, post data, please click on the related links below

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment