Node.js querystring Module
querystring is a built-in core module of Node.js, dedicated to parsing and formatting URL query strings. A query string is the part of a URL after the question mark (?) and usually consists of key-value pairs, for example:?name=John&age=30。
This module is very practical in scenarios such as front-end/back-end data transmission and API development, helping developers easily handle URL parameters.
Core Method Analysis
querystring.parse(str[, sep[, eq[, options]]])
Parses a query string into a JavaScript object.
Parameter Description:
str: the query string to parsesep(optional): the delimiter used to separate key-value pairs, default is'&'eq(optional): the delimiter used to separate keys and values, default is'='options(optional): a configuration objectmaxKeys: specifies the maximum number of keys to parse, default is 1000decodeURIComponent: the function used for decoding, default isquerystring.unescape()
Example code:
Example
const
querystring
=
require
(
'querystring'
)
;
const
query
=
'name=John&age=30&city=New+York'
;
const
parsed
=
querystring.
parse
(
query
)
;
console.
log
(
parsed
)
;
// Output: { name: 'John', age: '30', city: 'New York' }
querystring.stringify(obj[, sep[, eq[, options]]])
Serializes a JavaScript object into a query string.
Parameter Description:
obj: the object to serializesep(optional): the key-value pair delimiter, default is'&'eq(optional): the key-value delimiter, default is'='options(optional): a configuration objectencodeURIComponent: the function used for encoding, default isquerystring.escape()
Example code:
Example
const
querystring
=
require
(
'querystring'
)
;
const
obj
=
{
name
:
'John Doe'
,
age
:
25
,
occupation
:
'Software Developer'
}
;
const
stringified
=
querystring.
stringify
(
obj
)
;
console.
log
(
stringified
)
;
// Output: 'name=John%20Doe&age=25&occupation=Software%20Developer'
querystring.escape(str) and querystring.unescape(str)
These two methods are used to encode and decode special characters in a query string, respectively.
Example code:
Example
const
querystring
=
require
(
'querystring'
)
;
const
str
=
'Hello World!'
;
const
escaped
=
querystring.
escape
(
str
)
;
console.
log
(
escaped
)
;
// Output: 'Hello%20World%21'
const
unescaped
=
querystring.
unescape
(
escaped
)
;
console.
log
(
unescaped
)
;
// Output: 'Hello World!'
Practical Application Scenarios
1. Handling GET request parameters
Example
const
http
=
require
(
'http'
)
;
const
querystring
=
require
(
'querystring'
)
;
http.
createServer
(
(
req
,
res
)
=>
{
if
(
req.
url
.
includes
(
'?'
)
)
{
const
queryString
=
req.
url
.
split
(
'?'
)
[
1
]
;
const
params
=
querystring.
parse
(
queryString
)
;
console.
log
(
params
)
;
res.
end
(
`Received parameters
:
$
{
JSON.
stringify
(
params
)
}
`
)
;
}
else
{
res.
end
(
'No query parameters received'
)
;
}
}
)
.
listen
(
3000
)
;
2. Building API query strings
Example
const
querystring
=
require
(
'querystring'
)
;
const
apiParams
=
{
q
:
'node.js'
,
page
:
1
,
limit
:
10
,
sort
:
'desc'
}
;
const
apiUrl
=
`https
:
//api.example.com/search?${querystring.stringify(apiParams)}`;
console.
log
(
apiUrl
)
;
// Output: 'https://api.example.com/search?q=node.js&page=1&limit=10&sort=desc'
Notes
-
Encoding issues: By default, querystring encodes/decodes special characters. If you need custom encoding, you can pass in
encodeURIComponentordecodeURIComponentfunction in Vue 3. -
Data Type: All parsed values are of string type and need to be manually converted to other types (such as numbers, booleans, etc.).
-
Nested objects: The querystring module does not support parsing and serialization of nested objects. If you need to handle complex data structures, consider using JSON format.
-
URL-safe characters: spaces are encoded as
+instead of%20, which is different from theencodeURIComponentbehavior.
Alternatives
Although the querystring module is practical, newer versions of Node.js recommend using the URL and URLSearchParams APIs, which provide more modern and comprehensive URL handling capabilities.
Example
// Example using URLSearchParams
const
params
=
new
URLSearchParams
(
'name=John&age=30'
)
;
console.
log
(
params.
get
(
'name'
)
)
;
// Output: 'John'
Summary
The querystring module is a great tool for handling query strings in Node.js, especially for simple key-value pair data. Mastering its core methods (parse, stringify, escape, unescape) can greatly improve development efficiency. For more complex URL handling needs, consider using the URL and URLSearchParams APIs.
Node.js Built-in Modules