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);
[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> 用于界定查询字符串中的键值对的子字符串。默认为 '&'。 eq <String> 用于界定查询字符串中的键与值的子字符串。默认为 '='。 options <Object> decodeURIComponent <Function> 当解码查询字符串中百分号编码的字符时使用的函数。默认为 querystring.unescape() maxKeys <number> 指定要解析的键的最大数量。默认为 1000。指定为 0 则移除键数的限制
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 the sub-section of the key-value pairs in the query string. 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 the substring of the key and value in the query 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] 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('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 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> 用于界定查询字符串中的键值对的子字符串。默认为 '&' eq <String> 用于界定查询字符串中的键与值的子字符串。默认为 '=' options encodeURIComponent <Function> 当把对URL不安全的字符转换成查询字符串中的百分号编码时使用的函数。默认为 querystring.escape()
var querystring = require('querystring'); //'foo=bar&baz=qux&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.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('querystring'); console.log(encodeURIComponent('测试'));//%E6%B5%8B%E8%AF%95 console.log(querystring.escape('测试'));//%E6%B5%8B%E8%AF%95
[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('querystring'); console.log(decodeURIComponent('%E6%B5%8B%E8%AF%95'));//'测试' console.log(querystring.unescape('%E6%B5%8B%E8%AF%95'));//'测试'
The data requested by get is stored in the URL
http://127.0.0.1:8080/home/test?a=1&b=2
var http = require('http'); var url = require('url'); var querystring = require('querystring'); http.createServer(function(req,res){ var urlObj = url.parse(req.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);
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('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' } }); }).listen(8080);
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!