While the url
module can handle query strings attached to URLs, it can also be done with the built-in querystring
module. The querystring
module is dedicated to providing utilities solely focused on parsing and formatting URL query strings. As such, the module provides a much smaller number of methods to use. The core methods are listed below:
.parse()
: This method is used for parsing a URL query string into a collection of key-value pairs. The.decode()
method does the same.const str = 'prop1=value1&prop2=value2'; querystring.parse(str); // Returns { prop1: value1, prop2: value2}.stringify()
: This method is used for producing a URL query string from a given object via iteration of the object’s “own properties.” The.encode()
method does the same.const props = { "prop1": value1, "prop2": value2}; querystring.stringify(props); // Returns 'prop1=value1&prop2=value2'.escape()
: This method is used for performing percent-encoding on a given query string..unescape()
: This method is used to decode percent-encoded characters within a given query string.
The querystring
module is focused solely on manipulating URL query strings, so it requires the query string to have already been isolated from an incoming URL as part of a request. This means that some pre-processing of the URL is necessary before being able to use the module.
Additionally, it is worth noting that in the latest versions of Node (v16.x) the querystring
module has become a legacy feature, with its core functionality having been absorbed into the url
module via the URLSearchParams
API. However, the features in the querystring
module are still handy when using the long-term support versions of Node.js (v14.x).
Instructions
Import the querystring
module using require()
and save it to a const
variable called querystring
.
Isolate the query string from the url
variable using the .split()
method. Assign the resulting query string to a const
variable called queryToParse
.
Next, parse the isolated query string into an object of key/value pairs, assigning it to a const
variable called parsedQuery
.
Let’s add an extra property to the parsedQuery
object called exercise
, assigning it a value of 'querystring'
.
Then, convert the parsedQuery
object back into an encoded query string, assigning the value to a const
variable named modifiedQueryString
.