Source Code: lib/querystring.js
The node:querystring module provides utilities for parsing and formatting URL\nquery strings. It can be accessed using:
node:querystring
const querystring = require('node:querystring');\n
The querystring API is considered Legacy. While it is still maintained,\nnew code should use the <URLSearchParams> API instead.
querystring
The querystring.decode() function is an alias for querystring.parse().
querystring.decode()
querystring.parse()
The querystring.encode() function is an alias for querystring.stringify().
querystring.encode()
querystring.stringify()
The querystring.escape() method performs URL percent-encoding on the given\nstr in a manner that is optimized for the specific requirements of URL\nquery strings.
querystring.escape()
str
The querystring.escape() method is used by querystring.stringify() and is\ngenerally not expected to be used directly. It is exported primarily to allow\napplication code to provide a replacement percent-encoding implementation if\nnecessary by assigning querystring.escape to an alternative function.
querystring.escape
The querystring.parse() method parses a URL query string (str) into a\ncollection of key and value pairs.
For example, the query string 'foo=bar&abc=xyz&abc=123' is parsed into:
'foo=bar&abc=xyz&abc=123'
{\n foo: 'bar',\n abc: ['xyz', '123']\n}\n
The object returned by the querystring.parse() method does not\nprototypically inherit from the JavaScript Object. This means that typical\nObject methods such as obj.toString(), obj.hasOwnProperty(), and others\nare not defined and will not work.
Object
obj.toString()
obj.hasOwnProperty()
By default, percent-encoded characters within the query string will be assumed\nto use UTF-8 encoding. If an alternative character encoding is used, then an\nalternative decodeURIComponent option will need to be specified:
decodeURIComponent
// Assuming gbkDecodeURIComponent function already exists...\n\nquerystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null,\n { decodeURIComponent: gbkDecodeURIComponent });\n
The querystring.stringify() method produces a URL query string from a\ngiven obj by iterating through the object's \"own properties\".
obj
It serializes the following types of values passed in obj:\n<string> | <number> | <bigint> | <boolean> | <string[]> | <number[]> | <bigint[]> | <boolean[]>\nThe numeric values must be finite. Any other input values will be coerced to\nempty strings.
querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });\n// Returns 'foo=bar&baz=qux&baz=quux&corge='\n\nquerystring.stringify({ foo: 'bar', baz: 'qux' }, ';', ':');\n// Returns 'foo:bar;baz:qux'\n
By default, characters requiring percent-encoding within the query string will\nbe encoded as UTF-8. If an alternative encoding is required, then an alternative\nencodeURIComponent option will need to be specified:
encodeURIComponent
// Assuming gbkEncodeURIComponent function already exists,\n\nquerystring.stringify({ w: '中文', foo: 'bar' }, null, null,\n { encodeURIComponent: gbkEncodeURIComponent });\n
The querystring.unescape() method performs decoding of URL percent-encoded\ncharacters on the given str.
querystring.unescape()
The querystring.unescape() method is used by querystring.parse() and is\ngenerally not expected to be used directly. It is exported primarily to allow\napplication code to provide a replacement decoding implementation if\nnecessary by assigning querystring.unescape to an alternative function.
querystring.unescape
By default, the querystring.unescape() method will attempt to use the\nJavaScript built-in decodeURIComponent() method to decode. If that fails,\na safer equivalent that does not throw on malformed URLs will be used.
decodeURIComponent()