HI WELCOME TO SIRIS

Node.js Query String

Leave a Comment
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').

Node.js Query String Methods

The Node.js Query String utility has four methods. The two important methods are given below.
MethodDescription
querystring.parse(str[, sep][, eq][, options])converts query string into JSON object.
querystring.stringify(obj[, sep][, eq][, options])converts JSON object into query string.

Node.js Query String Example 1: parse()

Let's see a simple example of Node.js Query String parse() method.
File: query-string-example1.js
  1. querystring = require('querystring');  
  2. const obj1=querystring.parse('name=sonoo&company=kansiris');  
  3. console.log(obj1);  
Node.js query string example 1

Node.js Query String Example 2: stringify()

Let's see a simple example of Node.js Query String stringify() method.
File: query-string-example2.js
  1. querystring = require('querystring');  
  2. const qs1=querystring.stringify({name:'sonoo',company:'kansiris'});  
  3. console.log(qs1);  
Node.js query string example 2

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.