Updated April 14, 2023
Introduction to Node.js Query String
The following article provides an outline for Node.js Query String. Node.js has a module called query string. This Module is basically used for formatting and parsing URL. Before using this module we have to import it. In simple language we can say that we are extracting the URL in expected format. This makes the use of making query easier. We can say that it’s a way of adding additional data in the form of key value pair to the url. Which is nothing but the http request we are querying. Every time we request to the url we are looking for something particular, that particular thing can be queried in url itself.
Working of Node.js Query String
This module is mostly used in Node.js version 6 and later. Also you need to have latest version of the browser also such as chrome, Mozilla firefox, safari etc. There are some cases where you need to use older version of query string and browser or nodejs. At this time make sure you use the version of query string is 5.
Run the following command to get the lower version of query string.
Code:
npm install query-string@5
And enter. This command will install older version of query string. The main purpose of this module is to parse and decode the query string which are passed in URL. This is very important when we are working with different routs. URL components are getting decoded by decode uri component.
Now let’s see how it exactly works.
Let’s take one url.
Suppose we have following URL:
www.example.com/math/ex1?question=1
In above url you can see /ex1 then question mark and question = 1. Here we are querying the get request for question 1 from ex1 of the math. By this way we can parse the request and get the data as we required. If you want more data then you can add more key value pairs separated by “&” symbol.
Example with multiple parameters:
www.example.com/student?rollno=101&class=b
Query string starts with the question mark and then we added the parameters as required. And if there are more than one parameter we concate them with amp(&) symbol.
js Query String Methods
While working with the query string we find that there are many methods associate with this. First of all we need to import this module in our application.
Syntax for this is same as we are importing other modules in node js.
Syntax:
constqs = require('querystring');
Example:
Code:
varqs = require('querystring');
varqueryEx = qs.parse('rollno=101&class=b');
console.log(queryEx.rollno);
Output:
Given below are some of the methods in query string:
- querystring.unescape(str)
- querystring.decode()
- querystring.stringify(obj[, sep[, eq[, options]]])
- querystring.encode()
- querystring.parse(str[, sep[, eq[, options]]])
- querystring.escape(str)
1. querystring.unescape(str)
It helps t parsrurl of the string type.this method is not used directly. This method is used by querystring.parse() method. querystring.unescape(str) method uses javascript’sdecodeURIComponent() method. It makes use of this method to decode.
2. querystring.decode()
This method work similar as querystring.parse() method.
3. querystring.stringify(obj[, sep[, eq[, options]]])
This method has several parameters.
- obj: This is an object we are working with.
- sep: This is used as a delimiter to separate, which is an &.
- eq: This is delimiter again to get separate values. Which is = sign in this case.
- options: Here we are using to covert URL in safe mode with using function querystring.escape().
Now lets see how it works.
Example:
Code:
querystring.stringify({ ex1: 'math', science: ['one', 'two'], subject: '' });
Output:
So this will give us a query string created as we expect for url.
4. querystring.encode()
This method works same as querystring.stringify () method.
5. querystring.parse(str[, sep[, eq[, options]]])
This method also have same parameters as querystring.stringify() method. This method converts the querystring into key value pair. It returns the collection in readable format of key value pairs.
Example:
Code:
constqueryStr =querystring.parse('sub=math&ex=1');
console.log(queryStr);
Output:
In above example we used parsed method to get output in key value pair.
6. querystring.escape(str)
This method is having string parameter. This method is used to percent-encode. This is used to achieve the specific url query string. This method is not used directly. This method should be used querystring.stringify() method.
With all above methods we can do a little more to get comfortable results. Suppose we have query string and we are not willing to have any of the null values between it. Now for getting the url without null values can be possible with skip null parameter.
Example:
Code:
varqs = require('querystring');
qs.stringify({ ex1: 'null', science: 'one', subject: 'math'}, {
skipNull: true
});
Output:
Similarly we have concept of false values. At one point you may need to put a key without assigning any value to it. At this point you need to assign something like null, undefined or empty value for that. To get more insight about this lets check following example.
Below example is for false values.
Code:
varqs = require('query-string');
qs.stringify({ ex1: false, science: 'one', subject: 'math'});
Output:
Below example is for checking null.
Code:
varqs = require('query-string');
qs.stringify({ ex1: null, science: 'one', subject: 'math'});
Output:
And the last example is for undefined values.
Code:
varqs = require('query-string');
qs.stringify({ ex1: undefined, science: 'one', subject: 'math'});
Output:
By defaultNode.js query string has utf-8 encoding. If other than utf-8 encoding is used in application then as per the respecting encoding decode uri component will be used.
Conclusion
This module is very useful when we work with API (Application programming interface) calls. We may get different requirements to parse the url. This module gives us a easy way of doing things with url. As you have seen different methods of nodedejsquery string.
Recommended Articles
This is a guide to Node.js Query String. Here we discuss the introduction to Node.js Query String, along with working and js string method. You may also have a look at the following articles to learn more –