Home > Web Front-end > JS Tutorial > Share the introduction of queryString in NodeJS

Share the introduction of queryString in NodeJS

零下一度
Release: 2017-06-26 13:37:13
Original
1443 people have browsed it

Previous words

Whether it is front-end or back-end, a common application scenario is the processing of parameters in URLs. 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

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

The querystring.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 则移除键数的限制
Copy after login
var querystring = require('querystring');var str = 'foo=bar&abc=xyz&abc=123';
console.log(querystring.parse(str));//'{ foo: 'bar', abc: [ 'xyz', '123' ] }'
Copy after login

The second parameter is used to define the substring of the key-value pair 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' ] }
Copy after login

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

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

[Note] The object returned by the querystring.parse() method does not inherit from JavaScript’s 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 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' }));
Copy after login

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

The querystring.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()
Copy after login
var querystring = require('querystring');//'foo=bar&baz=qux&baz=quux&corge='console.log(querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' }));
Copy after login
var querystring = require('querystring');//'foo:bar;baz:qux'console.log(querystring.stringify({foo: 'bar', baz: 'qux'}, ';', ':'));
Copy after login

Encoding

【querystring.escape(str)】

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

The 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 the encoding implementation can be rewritten by assigning a function to querystring.escape when needed

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

[querystring.unescape(str)]

The querystring.unescape() method decodes the URL percent-encoded characters on the given str

The querystring.unescape() method is used by querystring.parse() and is usually not used be 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'));//'测试'
Copy after login

GET

get The requested data is stored in the URL

http://127.0.0.1:8080/home/test?a=1&b=2
Copy after login
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);
Copy after login

POST

The data requested by post will be written to the buffer , 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);
Copy after login

The above is the detailed content of Share the introduction of queryString in NodeJS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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