1+ /***
2+ * The goal of this file is to know about the query string functions;
3+ * This module provides utilities for dealing with query strings..
4+ *
5+ * How to run this example:
6+ * 1. node queryString.js
7+ * 2. See the message get displayed on prompt.
8+ */
9+
10+ var queryString = require ( 'querystring' ) ;
11+ /***
12+ * 'foo=bar&baz=qux&baz=quux&corge='
13+ */
14+ var qString = queryString . stringify ( {
15+ email : 'ashwinh@cybage.com' ,
16+ itemsCode : [ 'p001' , 'p002' ] ,
17+ payment : true
18+ } ) ;
19+
20+ console . log ( '\n' ) ;
21+ console . log ( qString ) ;
22+ console . log ( typeof qString ) ;
23+
24+ console . log ( '\n' ) ;
25+
26+ /***
27+ * 'foo:bar;baz:qux'
28+ */
29+ qString = queryString . stringify ( {
30+ firstname : 'Ashwin' ,
31+ lastname : 'Hegde'
32+ } , ';' , ':' ) ;
33+ console . log ( qString ) ;
34+ console . log ( typeof qString ) ;
35+
36+ console . log ( '\n' ) ;
37+
38+ /***
39+ * Escape method
40+ */
41+ qString = queryString . escape ( 'email=ashwinh@cybage.com&itemsCode=p001&itemsCode=p002&payment=true' ) ;
42+ console . log ( qString ) ;
43+ console . log ( typeof qString ) ;
44+
45+ console . log ( '\n' ) ;
46+
47+ /***
48+ * Parsing foo=bar&baz=qux&baz=quux&corge
49+ * Note: Options object may contain maxKeys property (equal to 1000 by default),
50+ * it'll be used to limit processed keys.
51+ * Set it to 0 to remove key count limitation.
52+ */
53+ qString = queryString . parse ( 'email=ashwinh%40cybage.com&itemsCode=p001&itemsCode=p002&payment=true' ) ;
54+ console . log ( '%j' , qString ) ;
55+ console . log ( typeof qString ) ;
56+
57+ console . log ( '\n' ) ;
58+
59+ /***
60+ * Unescape method
61+ */
62+ qString = queryString . unescape ( 'email=ashwinh%40cybage.com&itemsCode=p001&itemsCode=p002&payment=true' ) ;
63+ console . log ( '%j' , qString ) ;
64+ console . log ( typeof qString ) ;
0 commit comments