1. String conversion
Basic Introduction to the Query string module
The Query string module is used to convert the URL parameter string to the Parameter object, provides some useful functions such as "stringify", "parse", and so on to deal with the string, through serialization and deserialization, to better deal with the actual development of the condition requirements, For the logic of the processing also provides a good help, below let us learn it together!
2. Basic usage of serialized stringify function
The purpose of the Stringify function is to serialize the object, that is, to convert the object type to a string type (the default delimiter ("&") and the quantifier ("="), and this section first describes its basic usage, and in the next section we will learn how to replace the default Let's take a look at the following examples to understand clearly!
Example 1:querystring.stringify ("Object")
var querystring= require (' querystring '); var result = Querystring.stringify ({foo: ' bar ', cool:[' Xux ', ' Yys ']}); Console.log (result);
Operation Result:
Foo=bar&cool=xux&cool=yys
Look at the above introduction and examples, I believe you have a preliminary understanding of the Stringify function, quick strike, hands-on practice!
"Get=fire&get=ice&want=go", with stringify serialization how to achieve it?
Converts an object into a query string, typically using the default & Symbol and = equals sign to construct the string.
3. Multi-parameter usage of stringify function of serialized "multi-parameter"
In this section we will learn the multi-parameter usage of the Stringify function, the last section we know that the object is serialized as a string after the default is by the delimiter ("&") and the allocation ("=") of the composition, that can be changed, this section we come to understand, whether you could define the combination of the results, Look at the following small example
Example 1:querystring.stringify ("Object", "delimiter", "quantifier")
var querystring = require (' querystring '); var result = Querystring.stringify ({foo: ' bar ', cool:[' Xux ', ' Yys ']}, ' * ', ' $ '); Console.log (result);
Operation Result:
' Foo$bar*cool$xux*cool$yys '
Look at the above example, I believe you have been very clear, the delimiter, the allocation can be based on different circumstances and choose a given, then try it yourself!
"Spr#[email protected] #[email protected] #win" practice a practice!
Unlike 2, we can arbitrarily change the connection symbol used by the constructed string.
4. Basic usage of deserialization parse function
Just now we have learned the function of stringify functions, and then we will learn the--parse function of the inverse serialization function, the function of the parse function is to deserialize the string (by default, "=" "&" stitching), the conversion of an object type. The following example:
Example 1:querystring.parse ("string")
var querystring = require (' querystring '); var result = Querystring.parse (' Foo=bar&cool=xux&cool=yys '); Console.log (result);
Operation Result:
{foo: ' Bar ', cool: [' xux ', ' Yys ']}
Learning the "stringify function", the "parse function" is a piece of cake! Try it!
The stringify, parse function implements the conversion of strings to objects.
5. Multi-parameter usage of the parse function
Now that we are learning the extended use of the parse function, unlike the multi-parameter usage of the Stringify function in the previous section, the parse function can deserialize the string according to the user's custom delimiter and the delimiter, thus obtaining the corresponding object result. The following example:
Example 1:querystring.parse ("string", "delimiter", "quantifier")
var querystring = require (' querystring '); var result = Querystring.parse (' [Email protected][email protected][email Protected] ', ' @ ', ' $ '); Console.log (result);
Operation Result:
{foo: ', bar: ' Cool ', Xux: ' Cool ', Yys: '}
Look at the above example, I believe you have a bit of a temptation, and quickly pass a string parse it!
A multi-parameter usage of the parse function, which can be used to convert a self-defined string to get the value of an object by deserializing it.
6. Summary of Courses
I'm glad you've finished this lesson, here are some common methods of querystring, if you would like to know more, please refer to the following address:
Http://nodejs.cn/api/querystringhttp://nodeapi.ucdok.com/#/api/querystring.html
node. JS Learning Day Sixth--query String