Request query parsing
Decode query parameters received by a Node.js backend.
Node.js · Module
The node:querystring module parses URL query strings into key-value collections and formats objects back into query-string text. It also exposes encoding and decoding helpers.
The module parses URL query strings into collections of key-value pairs and formats objects into URL query strings. Repeated keys become arrays when parsed, while stringify iterates over an object's own properties and encodes values.
const querystring = require('node:querystring');
querystring.parse(str[, sep[, eq[, options]]]);
querystring.stringify(obj[, sep[, eq[, options]]]);
querystring.escape(str);
querystring.unescape(str);| Name | Type | Description |
|---|---|---|
str | string | Query-string text to decode. |
obj | object | Object whose own properties are serialized. |
sep | string | Optional field separator used during parsing or formatting. |
eq | string | Optional key/value assignment character used during parsing or formatting. |
options | object | Optional parsing or formatting options, including custom encoding functions. |
parse() returns a collection of key-value pairs; stringify() returns a URL query string; escape() returns a percent-encoded string; unescape() returns a decoded string.
node:querystring specifier rather than installing a package.const querystring = require('node:querystring');
const parsed = querystring.parse('term=node&tag=api&tag=backend');
const encoded = querystring.stringify({ term: 'node', tag: ['api', 'backend'] });
console.log(parsed.term); // 'node'
console.log(parsed.tag); // [ 'api', 'backend' ]
console.log(encoded); // term=node&tag=api&tag=backendParse an incoming query string into key-value data, then serialize an object back into query-string form.
Decode query parameters received by a Node.js backend.
Generate query strings for redirects, links, or outbound requests.
Apply Node-optimized percent encoding to an individual query value.
Import the module with require('node:querystring'). Use parse() to turn a query string into key-value data and stringify() to encode an object as a query string.
Follow-up: When would you choose URLSearchParams instead? Choose it when standardized behavior or browser-code compatibility is more valuable than this module's performance advantage.
querystring.parse(str) converts a query string into a collection of key-value pairs. A key occurring multiple times is represented as an array in the parsed result.
Follow-up: How should a consumer handle a field that may be either a string or an array? Normalize the value at the boundary before applying application logic.
querystring.stringify(obj) creates a query string by iterating over the object's own properties. Arrays produce repeated key-value pairs, and values are percent-encoded for the query-string format.
Follow-up: What should be checked when serializing unusual values? Verify the resulting string for the intended representation and use a custom encoding function only when the documented defaults are unsuitable.
querystring.parse()querystring.stringify()querystring.escape()querystring.unescape()URLSearchParams