Home > Web Front-end > JS Tutorial > body text

Detailed explanation of queryString module in nodejs

青灯夜游
Release: 2021-02-23 09:56:49
forward
2896 people have browsed it

Detailed explanation of queryString module in nodejs

Related recommendations: "nodejs Tutorial"

Whether it is the front-end or the back-end, a common application scenario is the processing of parameters in the URL. The nodeJS queryString module provides some tools for processing query strings. This article will introduce in detail queryString

var querystring = require('querystring');
/*
{ unescapeBuffer: [Function],
  unescape: [Function: qsUnescape],
  escape: [Function],
  encode: [Function],
  stringify: [Function],
  decode: [Function],
  parse: [Function] }
 */
console.log(querystring);
Copy after login

serialization in nodeJS

[querystring.parse(str[, sep[, eq[, options]]])】

 querystring The .parse() method can parse a URL query string (str) into a collection of key-value pairs. The parameters are as follows

str <String> 要解析的 URL 查询字符串。
sep <String> 用于界定查询字符串中的键值对的子字符串。默认为 &#39;&&#39;。
eq <String> 用于界定查询字符串中的键与值的子字符串。默认为 &#39;=&#39;。
options <Object>
    decodeURIComponent <Function> 当解码查询字符串中百分号编码的字符时使用的函数。默认为 querystring.unescape()   
maxKeys <number> 指定要解析的键的最大数量。默认为 1000。指定为 0 则移除键数的限制
Copy after login
var querystring = require(&#39;querystring&#39;);
var str = &#39;foo=bar&abc=xyz&abc=123&#39;;
console.log(querystring.parse(str));//&#39;{ foo: &#39;bar&#39;, abc: [ &#39;xyz&#39;, &#39;123&#39; ] }&#39;
Copy after login

The second parameter is used to define the sub-section of the key-value pairs in the query string. String

var querystring = require(&#39;querystring&#39;);
var str = &#39;foo=bar&abc=xyz&abc=123&#39;;
console.log(querystring.parse(str,&#39;a&#39;));//{ foo: &#39;b&#39;, &#39;r&&#39;: &#39;&#39;, bc: [ &#39;xyz&&#39;, &#39;123&#39; ] }
Copy after login

The third parameter is used to define the substring of the key and value in the query string

var querystring = require(&#39;querystring&#39;);
var str = &#39;foo=bar&abc=xyz&abc=123&#39;;
console.log(querystring.parse(str,&#39;&&#39;,&#39;c&#39;));//{ &#39;foo=bar&#39;: &#39;&#39;, ab: [ &#39;=xyz&#39;, &#39;=123&#39; ] }
Copy after login

[Note] The object returned by the querystring.parse() method does not inherit from JavaScript Object. This means that typical Object methods such as obj.toString(), obj.hasOwnProperty(), etc. are not defined and cannot be used

By default, percent-encoded characters in the query string Will be considered to use UTF-8 encoding. If another character encoding is used, the decodeURIComponent option needs to be specified

var querystring = require(&#39;querystring&#39;);
//{ w: &#39;����&#39;, foo: &#39;bar&#39; }
console.log(querystring.parse(&#39;w=%D6%D0%CE%C4&foo=bar&#39;, null, null,{ decodeURIComponent: &#39;gbkDecodeURIComponent&#39; }));
Copy after login

[querystring.stringify(obj[, sep][, eq][, options])】

 querystring The .stringify() method is the reverse operation of the querystring.parse() method. It generates a URL query string from a given obj by traversing the object's own properties. The parameters are as follows

obj <Object> 要序列化成一个 URL 查询字符串的对象
sep <String> 用于界定查询字符串中的键值对的子字符串。默认为 &#39;&&#39;
eq <String> 用于界定查询字符串中的键与值的子字符串。默认为 &#39;=&#39;
options
    encodeURIComponent <Function> 当把对URL不安全的字符转换成查询字符串中的百分号编码时使用的函数。默认为 querystring.escape()
Copy after login
var querystring = require(&#39;querystring&#39;);
//&#39;foo=bar&baz=qux&baz=quux&corge=&#39;
console.log(querystring.stringify({ foo: &#39;bar&#39;, baz: [&#39;qux&#39;, &#39;quux&#39;], corge: &#39;&#39; }));
Copy after login
var querystring = require(&#39;querystring&#39;);
//&#39;foo:bar;baz:qux&#39;
console.log(querystring.stringify({foo: &#39;bar&#39;, baz: &#39;qux&#39;}, &#39;;&#39;, &#39;:&#39;));
Copy after login

Encoding

【querystring.escape(str)】

The querystring.escape() method performs URL percent encoding on the given str, the same as the encodeURIComponent method

querystring.escape() Method is used by querystring.stringify() and is usually not used directly. The reason why it is open to the outside world is that when needed, the encoding implementation can be rewritten by assigning a function to querystring.escape

var querystring = require(&#39;querystring&#39;);
console.log(encodeURIComponent(&#39;测试&#39;));//%E6%B5%8B%E8%AF%95
console.log(querystring.escape(&#39;测试&#39;));//%E6%B5%8B%E8%AF%95
Copy after login

[querystring.unescape(str)]

Querystring.unescape () method performs decoding of URL percent-encoded characters on the given str

The querystring.unescape() method is used by querystring.parse() and is usually not used directly. The reason why it is open to the outside world is that the decoding implementation can be overridden when needed by assigning a function to querystring.unescape.

The querystring.unescape() method uses JavaScript's built-in decodeURIComponent() method to decode by default

var querystring = require(&#39;querystring&#39;);
console.log(decodeURIComponent(&#39;%E6%B5%8B%E8%AF%95&#39;));//&#39;测试&#39;
console.log(querystring.unescape(&#39;%E6%B5%8B%E8%AF%95&#39;));//&#39;测试&#39;
Copy after login

GET

The data requested by get is stored in the URL

http://127.0.0.1:8080/home/test?a=1&b=2
Copy after login
var http = require(&#39;http&#39;);
var url = require(&#39;url&#39;);
var querystring = require(&#39;querystring&#39;);
http.createServer(function(req,res){
    var urlObj = url.parse(req.url);
    var query = urlObj.query;
    var queryObj = querystring.parse(query);
    console.log(req.url);//&#39;/home/test?a=1&b=2&#39;
    console.log(query);//&#39;a=1&b=2&#39;
    console.log(queryObj);//{ a: &#39;1&#39;, b: &#39;2&#39; }
}).listen(8080);
Copy after login

POST

The data requested by post will be written into the buffer, and data splicing processing needs to be performed through the data event and end event of the request

var http = require(&#39;http&#39;);
var url = require(&#39;url&#39;);
var querystring = require(&#39;querystring&#39;);
http.createServer(function(req,res){
    var str = &#39;&#39;;  
    req.on(&#39;data&#39;, function(thunk){
        str += thunk;
    });
    req.on(&#39;end&#39;, function(){
        console.log(str);//&#39;name=a&email=b%40b.com&#39;
        var queryObj = querystring.parse(str);
        console.log(queryObj);//{ name: &#39;a&#39;, email: &#39;b%40b.com&#39; }
    }); 

}).listen(8080);
Copy after login

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of Detailed explanation of queryString module in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!