The Node.js Query String provides methods to deal with query string. It can be used to convert query string into JSON object and vice-versa.
To use query string module, you need to use require('querystring').
The Node.js Query String utility has four methods. The two important methods are given below.
Method | Description |
---|---|
querystring.parse(str[, sep][, eq][, options]) | converts query string into JSON object. |
querystring.stringify(obj[, sep][, eq][, options]) | converts JSON object into query string. |
Let's see a simple example of Node.js Query String parse() method.
File: query-string-example1.js
querystring = require('querystring'); const obj1=querystring.parse('name=sonoo&company=rookienerd'); console.log(obj1);
Let's see a simple example of Node.js Query String stringify() method.
File: query-string-example2.js
querystring = require('querystring'); const qs1=querystring.stringify({name:'sonoo',company:'rookienerd'}); console.log(qs1);