Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

node.js

What is querystring.parse in Node.js?

Amaaz Ahmad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The querystring.parse function is used to create an object of key-value pairs by parsing a URL query string. In simpler terms, it reads a query string and extracts a list of key-value pairs. We can find it in the Query String module of Node.js.

The querystring.parse function can be accessed in your program with:

const querystring = require("querystring");

Parameters

parse(queryStr[, sep[, eq[, options]]])

The function takes up to four parameters, as described below:

  • queryStr is the string that includes the URL query to be parsed.

  • sep is the substring used to delimit key and value pairs in the queryStr.

Default value for sep is &.

  • eq is the substring used to delimit key and value pairs in the queryStr.

Default value for eq is =.

  • options is an object which enables us to change the function’s behavior with respect to the maximum number of keys and percent-encoded characters. The options object can have the following keys and values:
    • decodeURIComponent: The function to use when decoding percent-encoded characters in the query string.

The querystring.unescape method is used as a default for decodeURIComponent.

  • maxKeys: This parameter defines the maximum number of keys that are allowed to be parsed.

The default value for maxKeys is 1000. To remove this limit, we can enter 0.

Apart from queryStr, all other parameters are optional paramters.

Return value

The parse function returns an object consisting of key-value pairs identified in the URL string.

It is important to note that the returned object is not from the class Object in JavaScript.

Example

// Import the querystring module
const querystring = require("querystring");
// defining the query string as per default parameters of the parse function
var str = "username=educative_user&password=a1b2c3";
// calling the parse function with default parameters
console.log("Parsed Object: ",querystring.parse(str))

A simple query string is parsed in the above example to create an object by identifying the key-value pairs. The following key-value pairs are defined in the query string:

  • username: educative_user
  • password: a1b2c3

This can be verified by the output as well.

RELATED TAGS

node.js

CONTRIBUTOR

Amaaz Ahmad
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring