Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Node.js urlObject.query API
Next article icon

Node.js Query String

Last Updated : 12 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The Query String module used to provides utilities for parsing and formatting URL query strings. It can be used to convert query string into JSON object and vice-versa. 

Node.js Query String

Query strings in Node.js are a common way to pass data to a server via a URL. A query string is the part of a URL that comes after a "?" symbol and contains key-value pairs separated by &. These strings are used in HTTP requests to send additional parameters to the server.

The Query String is the part of the URL that starts after the question mark(?).

Importing Module:

You can include the module using the following code:

const querystring = require('querystring');

Note: It’s not a global object, so need to install it explicitly. 

Install Module:

npm install querystring 

Example 1: Using parse()

JavaScript
// Filename - index.js  // Importing the models import url from 'url' import querystring from 'querystring'  // A URL is taken let exampleUrl =  'http://www.company.com:81/a/b/c.html?user=GEEKSFORGEEKS&year=2021#p2';  //Parse the whole URL let parsed_Url = url.parse(exampleUrl);  // Parse only querystring. let parsed_queryString = querystring.parse(parsed_Url.query);  // Print the result. console.log("This is parsed URL :",parsed_Url);  console.log("This is parsed Query String :",parsed_queryString); 

Output: 

This is parsed URL : Url {
protocol: 'http:',
slashes: true,
auth: null,
host: 'www.company.com:81',
port: '81',
hostname: 'www.company.com',
hash: '#p2',
search: '?user=GEEKSFORGEEKS&year=2021',
query: 'user=GEEKSFORGEEKS&year=2021',
pathname: '/a/b/c.html',
path: '/a/b/c.html?user=GEEKSFORGEEKS&year=2021',
href:
'http://www.company.com:81/a/b/c.html?user=GEEKSFORGEEKS&year=2021#p2'
}
This is parsed Query String : [Object: null prototype]
{ user: 'GEEKSFORGEEKS', year: '2021' }

Example 2: Using stringify(): 

JavaScript
// Importing the model import querystring from 'querystring'  // Specify the  object  // to be serialized  const q2=querystring.stringify({                             name:'Testing',                             company:'GeeksforGeeks',                             content:'Article',                             date:'9thMarch2021'                            });    // Print the result. console.log(q2);   

Output:  

name=Testing&company=GeeksforGeeks&
content=Article&date=9thMarch2021

Benefits

  • Query strings provide a simple way to pass small amounts of data to the server without needing to modify the URL or body of the request.
  • Query strings allow search engines to index pages with specific parameters, improving SEO.
  • Query strings are often used in combination with HTML forms, where form data is submitted via the URL.

Summary

The querystring module is essential for managing query strings in Node.js applications. It provides straightforward methods for parsing and formatting query strings, making it easier to handle URL parameters in web development.

Reference: https://nodejs.org/api/querystring.html


Next Article
Node.js urlObject.query API
author
_sh_pallavi
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • Node.js
  • Node.js- querystring-Module

Similar Reads

  • Node.js querystring.parse() Method
    The querystring.parse() method is used to parse a URL query string into an object that contains the key and pair values of the query URL. The object returned does not inherit prototypes from the JavaScript object, therefore usual Object methods will not work. During parsing, the UTF-8 encoding forma
    3 min read
  • Node.js querystring.stringify() Method
    The querystring.stringify() method is used to produce an URL query string from the given object that contains the key-value pairs. The method iterates through the object's own properties to generate the query string. It can serialize a single or an array of strings, numbers, and booleans. Any other
    2 min read
  • Node.js querystring.unescape() Method
    On the specified str, the querystring.unescape() method decodes URL percent-encoded characters. This method converts a percent-encoding string into a normal string. It means it decodes any percent-encoding string into a normal string by removing the % symbol. This method iterates through the string
    2 min read
  • Node.js Query String Complete Reference
    Node.js query String module is used as utilities for parsing and formatting URL query strings. It can be used to convert query string into JSON object and vice-versa. Example: C/C++ Code // Import the querystring module const querystring = require("querystring"); // Specify the URL query s
    1 min read
  • Node.js urlObject.query API
    The urlObject.query is the query string returned without the ASCII question mark (?) or an object returned by the query string module named as parse() method. The url.parse() method is used to check whether the query is a string or an object. Basically, the argument (parseQueryString) that is passed
    2 min read
  • Node.js URL.username API
    URL.username is an inbuilt application programming interface(API) of the URL class within Node.JS. URL.username API is used to get and set the username of the URL. Syntax: url.username URL: It is an object created by a URL constructor. Example 1: (Getting the username of URL) C/C++ Code //Creating a
    1 min read
  • Reading Query Parameters in Node
    In Node.js, query parameters are typically accessed from the URL of a GET request. When using the core HTTP module or a framework like Express, query parameters can be parsed and accessed for dynamic functionality. A query string refers to the portion of a URL (Uniform Resource Locator) that comes a
    2 min read
  • Node.js Request Module
    The request module in Node is used to make HTTP requests. It allows developers to simply make HTTP requests like GET, POST, PUT, and DELETE in the Node app. It follows redirects by default. Syntax:const request = require('request'); request(url, (error, response, body) => { if (!error &&
    2 min read
  • Reading Path Parameters in Node.js
    Path parameters are an essential part of RESTful APIs, allowing the server to capture values from the URL and use them in request handling. This capability is crucial for creating dynamic and flexible endpoints. In Node.js, especially when using frameworks like Express.js, handling path parameters i
    2 min read
  • Routing in NodeJS
    Routing is the process of deciding how a server should respond to different requests made by users. When you visit a website or use an app, your browser sends a request to the server. Routing determines how the server handles these requests based on the URL you visit and the type of request (such as
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences