NodeJS Query String: Usage and Examples

NodeJS Query String module is used to parse and format the URL query strings. In this article, we will learn about different methods provided by this module but let’s first understand URLs and Query String.

What is a URL? 

URL stands for Uniform Resource Locator, the location of a given unique resource of a website, resource can be an HTML page, CSS document, Images, etc.

http://domain/path/

What is a Query String?

Query String appears at the end of the URL, starting with a question mark character (?) used to send different types of requests to the server.

http://domain/path?query_string

NodeJS Query String Module

Node.js Query String is a built-in Node.js module, so it is not required to install it using npm, we just have to import it using the below syntax.

Syntax:

const querystring = require('querystring');

Methods of NodeJS Query String Module

There are various methods provided by the Query String Module used to perform the different operations, let’s see them one by one.

querystring.parse()

This method takes a query string as an argument and parse it into an object containing the value of the query string in the form of key-value pair. 

Syntax:

querystring.parse(str, sep, eq, options)
  • str: It is a mandatory field that contains the actual query string.
  • sep: It is a substring used to delimit the key and value pairs in the query string. It is an optional parameter, by default, the value is “&”.
  • eq: It is another substring used to delimit keys and values in the query string, by default the value is “=”.
  • options: We can pass two option to this method, the first one is decoderURIComponent, which define the encoding format and the second is maxKeys, which define the maximum number of keys that should be parsed. 

Example:

const querystring = require('querystring');
const dummyQueryString = 'author=Aditya&website=codeforgeeks.com';
const queryStringObj = querystring.parse(dummyQueryString);
console.log(queryStringObj);

Output:

Parse Method Example

querystring.stringify()

This method is the opposite of the method querystring.parse(), querystring.parse() is convert a query string into an object containing key-value pair, whereas this method produces a query string from the object containing key-value pair.

Syntax:

querystring.stringify(obj, sep, eq, options)
  • obj: It is a mandatory field that contains the object with key-value pair.
  • sep: It is a substring used to delimit the key and value pairs in the query string. It is an optional parameter, by default, the value is “&”
  • eq: It is another substring used to delimit keys and values in the query string, by default the value is “=”.
  • options: We can pass decoderURIComponent as an option, which defines the encoding format.

Example:

const querystring = require('querystring');
const queryStringObj = {
    author: 'Aditya',
    website: 'codeforgeeks.com'
};
const queryString = querystring.stringify(queryStringObj);
console.log(queryString);

Output:

Stringify Method Example

querystring.decode()

This method is similar to querystring.parse() method, used to get the object from the query string.

Example:

Let’s see an example, using both querystring.decode() and querystring.parse() method to get the object with key-value pair from a given query string to see the differences.

const querystring = require('querystring');
const dummyQueryString = 'author=Aditya&website=codeforgeeks.com';
const queryStringObj1 = querystring.parse(dummyQueryString);
const queryStringObj2 = querystring.decode(dummyQueryString);
console.log(queryStringObj1);
console.log(queryStringObj2);

Output:

Decode Method Exampple

The output of both methods is completely identical.

querystring.encode()

This method is similar to querystring.stringify() method, used to get the query string from the object containing key-value pair.

Example:

Let’s see an example, using both querystring.encode() and querystring.stringify() method to get the query string from a given object.

const querystring = require('querystring');
const queryStringObj = {
    author: 'Aditya',
    website: 'codeforgeeks.com'
};
const queryString1 = querystring.stringify(queryStringObj);
const queryString2 = querystring.encode(queryStringObj);
console.log("stringify method: "+ queryString1);
console.log("encode method: " + queryString2);

Output:

Encode Method Example

The output of both methods is completely identical.

querystring.escape()

This method replaces the whitespace of a string with the %20 and returns a percent-encoded query string.

Syntax:

querystring.escape(str)

where str is a query string.

Example:

const querystring = require("querystring")
const str = "Hello World!";
const result = querystring.escape(str);
console.log(result)

Output:

Escape Method Example

querystring.unescape()

This method replaces the %20 from a percent-encoded query string to whitespace and returns the standard query string.

Syntax:

querystring.unescape(str)

where str is a query string.

Example:

const querystring = require("querystring")
const str = "Hello%20World!";
const result = querystring.unescape(str);
console.log(result)

Output:

Unescape Method Example

Frequently Asked Questions (FAQs)

What is a query string in Node.js?

In Node.js query string is basically a build-in module that provides many methods by which we can parse and format URL query string.

Why use query strings?

Query string present at the end of a URL separate by a question mark character (?) used to send different types of requests to the server.

How to get the data from query string in Node?

To get data from the query string in Node.js we can use the querystring.parse() method that can take a query string as an argument and return an object containing the actual data in the form of key-value pairs.

What is the difference between query string and form data?

Whenever we make a get request to the server we use a query string to pass data after a question mark (“?”) at the end of a URL whereas when we make a post required we can send data as from data to the server in the request body in an encoded form separated from the URL for further process.

What is a query string example?

Query String appears at the end of the URL, starting with a question mark character (?). An example of a query string is http://domain/path?query_string.

How do you pass a value to a query string?

To pass a value to a query string you have to specify a key then use the equal sign(=) and then assign the respective value and for multiple key-value pairs you have to use the ampersand sign(&) to separate them.

How to decode query string in JavaScript?

Decoding a query string in JavaScript is a bit tricky, you have to first split the string using “&” and “=” then pass the value to the decodeURIComponent() function to get key-value pairs present in it. The more straightforward way to do this is to use the Node.js Query String module method querystring.parse() which directly returns key-value pairs.

What is a query string parameter?

A query string parameter is what you see at the end of URLs after a question mark (“?”) mainly used to specify additional data in the form of key-value pairs.

Summary

Node.js Query String is a useful module that helps parse and format the URL query strings. It also encodes an object into a query string and then again decodes the query string from the object. We hope this tutorial helped you to understand the Node.js Query String.

Also Read: Node.js Event-Driven Programming (With Example)

Reference

https://nodejs.org/api/querystring.html

Aditya Gupta
Aditya Gupta
Articles: 109