2. What is Query String in NodeJS
• There are ways to deal with query strings to the Node.js Query
String. It can transform a query string into a JSON object and the
other way around.
• Now, there are methods for parsing and formatting URL query strings
available in the Node.js query string module. The following command
will access the query string module:
• const querystring = require(‘querystring’);
3. var url = require('url');
var my_url = 'http://localhost:3000/index.htm?year=2020&month=May';
//Parsing the adress:
var que = url.parse(my_url, true);
//The parsing method has created an object that can be accessed using its property
names.
console.log("Host: "+ que.host);
console.log("PathName: "+que.pathname);
console.log("Search Query: "+que.search);
console.log("Protocol: "+que.protocol);
console.log("Href: "+que.href);
//You can find about the month inside the query by storing it inside a variable and
later accessing its properties.
var que_info = que.query;
console.log("Year: "+ que_info.year);
4.
5. In NodeJS the following are the six Query
String Parameters listed below.
• decode()
• encode()
• escape(str)
• parse(str[, sep[, eq[, options]]])
• stringify(obj[, sep[, eq[, options]]])
• unescape(str)
6. Query String methods in NodeJS with
Description
querystring.parse() Method in NodeJS
• The URL query string is parsed into an object with a key-value pair
using the querystring.parse() function.
• We cannot utilize Object methods like obj.toString or
obj.hasOwnProperty since the object we receive is not a JavaScript
object ().
• The syntax for the method is below.
• querystring.parse( str[, sep[, eq[, options]]]) )
7. • The syntax for the method is below.
• querystring.parse( str[, sep[, eq[, options]]]) )
• As seen from the above syntax this method accepts four parameters, which
are described below.
• str: This is the only required string field and it specifies in the query string that
has to be parsed.
• sep: If provided, it is an optional string parameter that specifies the substring that
will be used to separate the query string’s key and value pairs. The default value
used is “&”.
• eq: It is an optional string field that is used in the query string to separate the keys
from the values. The default value used is “=”.
• options: It is an optional object field that is used to modify the behavior of the
method. It can have the following parameters:
• decodeURIComponent: It is a function that would be used to provide the query string’s
encoding format. The default setting is querystring.unescape(), about which we will learn
later.
• maxKeys: This value indicates how many keys in total should be processed. Any number of
keys can be parsed if the value is set to “0,” which eliminates any counting restrictions. The
default setting is “1000.”
8. querystring.stringify() Method in NodeJS
• A supplied object that has a key-value pair can be utilized to create a
query string using the querystring.stringify() method. When compared
to querystring.parse(), it is the exact opposite.
• The string, integer, and Boolean values for the key can all be
converted using it. An array of strings, numbers, or Boolean values
can also be used as values. Serialization is the process of converting an
object to a query string.
• Unless we indicate an alternative encoding format, the most recent
UTF-8 encoding format is taken into account. But since UTF-8
encoding is the industry standard and includes all international
characters,
9. • The syntax for the method is below.
• querystring. stringify( obj[, sep[, eq[, options]]]) )
• The method accepts four parameters, which are specified below, as can be seen
from the syntax above.
• obj: The sole object field that is necessary defines the object that has to be
serialized.
• sep: It is a string field that is optional and, if provided, provides the substring that
will be used to separate the key and value pairs in the query string. “&” is the
default value, which is typically used.
• eq: The substring used to separate the query string’s keys and values are specified
in this optional string parameter. In most cases, the default value is “=”.
• options: It is an optional object field that is used to change how the method
behaves. It can have the following parameters:
• decodeURIComponent: It is a function that would be utilized to specify the query string’s
encoding format. The default value is querystring.escape(), about which we will learn later.
10. querystring.encode() Method in NodeJS
• A simple alias for the querystring.stringify() technique is querystring.encode(). We can use it in our
stringify example. Add the following code to the querystring.js file we previously established.
• // Import the querystring module
• const querystring = require(“querystring”);
• // Name the object that required serialisation.
• let obj = {
• name: “nabendu”,
• access: true,
• role: [“developer”, “architect”, “manager”],
• };
• // Use the stringify() method on the object
• let queryString = querystring.encode(obj);
• console.log(“Query String 1:”, queryString);
• Run the terminal command node querystring.js like we did earlier. And the results will match those
of the querystring.stringify() function.
• Query String 1: name=nabendu&access=true&role=developer&role=architect&role=manager
11. querystring.decode() Method in NodeJS
• A simple alias for the querystring.parse() function is querystring.decode().
We can use it in our parsing example. Add the following code to the
querystring.js file we previously established.
• // Import the querystring module
• const querystring = require(“querystring”);
• // Specify this URL query string to be parsed
• let urlQueryString =
“name=nabendu&units=kgs&units=pounds&login=false”;
• // Use the parse() method on the string
• let parsedObj = querystring.decode(urlQueryString);
• console.log(“Parsed Query 1:”, parsedObj);
• Run the node querystring.js command from a terminal, as you did before.
The result will be identical to that of the querystring.parse() function.
• Parsed Query 1: [Object: null prototype] { name: ‘bhanu’, units: [ ‘kgs’,
‘pounds’ ], login: ‘false’ }
12. • querystring.escape(str) Method in NodeJS
• The querystring.escape() function is typically only utilised by the
querystring.stringify() method.
• f.querystring.unescape(str) Method
• The querystring.unescape() method is often only utilised by the
querystring.parse() method..