NodeJs Tutorials
- NodeJs
- Install NodeJs
- Why use NodeJs
- NodeJs Process Model
- create First Application
- Run NodeJs Application
- Node.js Console
- Node.Js Modules
- URL Modules
- node.js Callback
- Node.js Events
- Upload Files
- Upload single & Multiple Files
- NodeJs File System
- NodeJs Email
- Debugging NodeJs
- .ENV
- NodeJs Mysql
- Helpers
- encription and decription in nodeJs
- Query string
- Date & Time
- Express Js
- Template Engine with Express
- MVC Pattern in Node.Js
- list of NPM Module
- Middleware
- Body Parser
- Render
- Nodemon module
- Morgan module
- Flash Message in ExpressJs
- Session
- Session store in database
- Cookies
- Helmet
- Multer
- Router: How To Use Routing In Node.Js
- App.Js
- express.json() and express.urlencoded()
- REST APIs in NodeJs
- Gloabal Objects
- Submit Form Data
- How to get Post Data in Node.js
- How to Get URL Parameters in Node.js
- How to create Node Project
- How to Insert Form Data Into the MySql Table Using Node.js
- How to fetch Data from MySQL database table using Node.js
- CRUD Example
- Await and Async
- Promises
- Login Example
- Password Encription
- How to validate Form (Form Validation) in Node.Js?
- Registration & Login form usingn Node.Js & MySql?
- Forgot & Reset Password
- File Upload in Node.Js with ExpressJs
- Resize Image Before Upload using Multer Sharp
- Upload multiple file using node.js with multer module
- Upload file using node.js with multer module
- Client IP Address
- REST API Downloading File in NodeJs
- Export and Download CSV From MySQL Database
- CRUD REST API
- CRUD REST API Example 2
- Enable HTTPS using Node
- How to send EMAIL using NodeJs?
- Dynamic dependent dropdown using NodeJs?
- Autocomplete Search
- Get and Send Data From Ajax Request
- Get and Post Data using Ajax
- Passport Authentication
- Node Js Data type
- Node Js Error
- Node Js Array Function
- Node Js String Function
- Puppeter Module
Query string in NodeJs?
The querystring module is used for parsing and formatting URL query strings. It can be used to convert query string into JSON object and vice-versa.
The Query String is the part of the URL that starts after the question mark(?). that assigns values to specified parameters.
npm install querystring
################# OR ##############
npm install query-string
You can include this module using following command.
const querystring = require('querystring');
Example
File Name :
https://ittutorial.in/controller/method?name=sana
https://ittutorial.in/controller/method?name=sana&id=07
here name=sana is query string.
Query String Methods :-
querystring.encode()
querystring.decode()
querystring.parse(str[, sep[, eq[, options]]])
querystring.stringify(obj[, sep[, eq[, options]]])
querystring.escape(str)
querystring.unescape(str)
querystring.parse() Method
The querystring.decode() method is nothing but an alias for querystring.parse() method.
The querystring.parse() method is used to parse the URL query string into an object that contains the key value pair. The object which we get is not a JavaScript object, so we cannot use Object methods like obj.toString, or obj.hasOwnProperty().
Syntax :- querystring.parse( str[, sep[, eq[, options]]]) )
str :- str is the string field that specifies the query string that has to be parsed.
sep :- It is an optional string field, it specifies the substring used to key and value pairs in the query string.
The default value which is generally used is "&"
eq :- The default value which is "="
option :- It is an optional object field which is used to modify the behaviour of the method.
Example
File Name :
var querystring = require('querystring');
var qry = querystring.parse('year=2020&month=may');
console.log(qry.year);
Example
// Import the querystring module
const querystring = require("querystring");
// Specify the URL query string to be parsed
let urlQueryString = "name=mahtab&age=36&id=07&login=false";
// Use the parse() method on the string
let parsedObj = querystring.parse(urlQueryString);
console.log("Parsed Query 1:", parsedObj);
// Use the parse() method on the string with sep as `&&` and eq as `-`
urlQueryString = "name-sana&&age-2&&id-07&&login-true";
parsedObj = querystring.parse(urlQueryString, "&&", "-");
console.log("\nParsed Query 2:", parsedObj);
// Specify a new URL query string to be parsed
urlQueryString = "type=admin&articles=node&articles=javascript&access=true";
// Use the parse() method on the string with maxKeys set to 1
parsedObj = querystring.parse(urlQueryString, "&", "=", { maxKeys: 1 });
console.log("\nParsed Query 3:", parsedObj);
// Use the parse() method on the string with maxKeys set to 2
parsedObj = querystring.parse(urlQueryString, "&", "=", { maxKeys: 2 });
console.log("\nParsed Query 4:", parsedObj);
// Use the parse() method on the string with maxKeys set to 0 (no limits)
parsedObj = querystring.parse(urlQueryString, "&", "=", { maxKeys: 0 });
console.log("\nParsed Query 5:", parsedObj);
querystring.encode() :- stringify()
The querystring.encode() function is an alias for querystring.stringify().
The querystring.stringify() method is used to produce a query string from a given object, which contains a key value pair. It is exactly the opposite of querystring.parse() Method.
File Name :
querystring. stringify( obj[, sep[, eq[, options]]]) )
Example
File Name :
querystring = require('querystring');
const qry = querystring.stringify({name:'sana',id:'07'});
console.log(qry);
Example
File Name :
// Import the querystring module
const querystring = require("querystring");
// Specify the object that needed to be serialized
let obj = {
name: "sana",
access: true,
role: ["developer", "admin", "manager"],
};
// Use the stringify() method on the object
let queryString = querystring.stringify(obj);
console.log("Query String 1:", queryString);
obj = {
name: "mahira",
access: false,
role: ["admin", "HR"],
};
// Use the stringify() method on the object with sep as `, ` and eq as `:`
queryString = querystring.stringify(obj, ", ", ":");
console.log("Query String 2:", queryString);
// Use the stringify() method on the object with sep as `&&&` and eq as `==`
queryString = querystring.stringify(obj, "&&&", "==");
console.log("\nQuery String 3:", queryString);