NodeJS Query String: Usage and Examples

Node JS Query String Thumbnail

NodeJS Query String is used to parse and format the URL query strings. 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.

Example:

http://domain/path/

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.

Example:

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, 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

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.

Reference

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