Most Popular Tutorials
Most Popular Tutorials :-

Simply Easy Learning at Your Fingertips. Click Tutorials Menu to view More Tutorial List





what is query string ?

The querystring module is used for parsing and formatting URL query strings. It can be used to convert query string into JSON object and vice-versa.

The Query String is the part of the URL that starts after the question mark(?). that assigns values to specified parameters.

npm install querystring
################# OR ##############
npm install query-string

You can include this module using following command.

const querystring = require('querystring');

Example

File Name :

https://ittutorial.in/controller/method?name=sana
https://ittutorial.in/controller/method?name=sana&id=07
here name=sana is query string.

Query String Methods :-

  • querystring.encode()
  • querystring.decode()
  • querystring.parse(str[, sep[, eq[, options]]])
  • querystring.stringify(obj[, sep[, eq[, options]]])
  • querystring.escape(str)
  • querystring.unescape(str)

  • querystring.parse() Method

    The querystring.decode() method is nothing but an alias for querystring.parse() method.

    The querystring.parse() method is used to parse the URL query string into an object that contains the key value pair. The object which we get is not a JavaScript object, so we cannot use Object methods like obj.toString, or obj.hasOwnProperty().

    Syntax :- querystring.parse( str[, sep[, eq[, options]]]) )
  • str :- str is the string field that specifies the query string that has to be parsed.
  • sep :- It is an optional string field, it specifies the substring used to key and value pairs in the query string. The default value which is generally used is "&"
  • eq :- The default value which is "="
  • option :- It is an optional object field which is used to modify the behaviour of the method.

  • Example

    File Name :

    var querystring = require('querystring');
    var qry = querystring.parse('year=2020&month=may');
    console.log(qry.year);

    Example

    // Import the querystring module
    const querystring = require("querystring");
    // Specify the URL query string to be parsed
    let urlQueryString = "name=mahtab&age=36&id=07&login=false";
    // Use the parse() method on the string
    let parsedObj = querystring.parse(urlQueryString);
    console.log("Parsed Query 1:", parsedObj);
    // Use the parse() method on the string with sep as `&&` and eq as `-`
    urlQueryString = "name-sana&&age-2&&id-07&&login-true";
    parsedObj = querystring.parse(urlQueryString, "&&", "-");
    console.log("\nParsed Query 2:", parsedObj);
    // Specify a new URL query string to be parsed
    urlQueryString = "type=admin&articles=node&articles=javascript&access=true";
    // Use the parse() method on the string with maxKeys set to 1
    parsedObj = querystring.parse(urlQueryString, "&", "=", { maxKeys: 1 });
    console.log("\nParsed Query 3:", parsedObj);
    // Use the parse() method on the string with maxKeys set to 2
    parsedObj = querystring.parse(urlQueryString, "&", "=", { maxKeys: 2 });
    console.log("\nParsed Query 4:", parsedObj);
    // Use the parse() method on the string with maxKeys set to 0 (no limits)
    parsedObj = querystring.parse(urlQueryString, "&", "=", { maxKeys: 0 });
    console.log("\nParsed Query 5:", parsedObj);

    querystring.encode() :- stringify()

    The querystring.encode() function is an alias for querystring.stringify().

    The querystring.stringify() method is used to produce a query string from a given object, which contains a key value pair. It is exactly the opposite of querystring.parse() Method.

    File Name :

    querystring. stringify( obj[, sep[, eq[, options]]]) )

    Example

    File Name :

    querystring = require('querystring');
    const qry = querystring.stringify({name:'sana',id:'07'});
    console.log(qry);

    Example

    File Name :

    // Import the querystring module
    const querystring = require("querystring");
    // Specify the object that needed to be serialized
    let obj = {
    name: "sana",
    access: true,
    role: ["developer", "admin", "manager"],
    };
    // Use the stringify() method on the object
    let queryString = querystring.stringify(obj);
    console.log("Query String 1:", queryString);

    obj = {
    name: "mahira",
    access: false,
    role: ["admin", "HR"],
    };
    // Use the stringify() method on the object with sep as `, ` and eq as `:`
    queryString = querystring.stringify(obj, ", ", ":");
    console.log("Query String 2:", queryString);

    // Use the stringify() method on the object with sep as `&&&` and eq as `==`
    queryString = querystring.stringify(obj, "&&&", "==");
    console.log("\nQuery String 3:", queryString);





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here