Friday, 18 May 2018

005 Node.js Query String

Node.js Query String

Any URL may have optional component call query string. The query string parse the webpage and can be useful to send static argument to the webpage. The query string is composed of key value pair.

Node.js contain module called querystring.
querystring module parse query get from web page URL.Here is the syntax how you will add
querystring module to your application.


var mydata = require('querystring');


The Node.js querystring module have following methods


  • stringify :stringify method serialize an object to a  query string. A query string content "&" to add more query and "=" to set query value.It is a serialization method.



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

    Example : querystring.stringify({myName: 'John', myAge: '20'})
    will result 'myName=John&myAge=20'
  • parse :Parse do parse , just  opposite of stringify. Parts deserialize query to object.
    syntax:querystring.parse(str[, sep[, eq[, options]]])

    Example :querystring.parse('myName=John&myAge=20')
   
will result {myName: 'John', myAge: '20'}
  • escape :The escape method performs encoding of URL percent-encoded characters on the given string. It generates a URL safe string. 
    syntax:querystring.escape(str)

     Example : querystring.escape('Date of Borth 30-03-2018 at 4.30 AM')
    will result Date%20of%20Borth%2030-03-2018%20at%204.30%20AM'


  • unescape :The usescape method performs decoding of URL percent-encoded characters on the given string. 
    syntax:querystring.unescape(str)


Example : querystring.unescape(
'Date%20of%20Borth%2030-03-2018%20at%204.30%20AM');

    will result 'Date of Borth 30-03-2018 at 4.30 AM'


Query String Example


Example 1(stringify)
var querystring = require('querystring');
var q=querystring.stringify({myName:'John',myAge:20}); 
console.log(q);



Output:
myName=John&myAge=20

Example 2 (Parse)
var querystring = require('querystring');
var q = querystring.parse('myName=John&myAge=20');
console.log("Name :"+q.myName);
console.log("Age :"+q.myAge);



Output:
Name :John
Age :20




Example 3 (escape)
var querystring = require('querystring');
var q=querystring.escape('Date of Borth 30-03-2018 at 4.30 AM'); 
console.log(q); 



Output:(URL Safe string)
Date%20of%20Borth%2030-03-2018%20at%204.30%20AM



Example 4 (unescape)
var querystring = require('querystring');var q=querystring.unescape('Date%20of%20Borth%
2030-03-2018%20at%204.30%20AM'); 
console.log(q); 


Output:(URL Safe string to original string)
Date of Borth 30-03-2018 at 4.30 AM

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...