VOOZH about

URL: https://www.geeksforgeeks.org/node-js/node-js-query-string/

⇱ Node.js Query String - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Node.js Query String

Last Updated : 28 Jan, 2026

In Node.js, a query string is the part of a URL used to send small key-value data to the server, mainly with GET requests for filtering and fetching information without using a request body.

  • Appears after "?" in the URL as key=value pairs, separated by &.
  • Parsed and formatted using querystring or modern URLSearchParams.
  • Commonly used for searching, filtering, sorting, and pagination.

Example URL:

http://localhost:8080?id=1&name=Nicol
  • id=1 and name=Nicol are query parameters.
  • Multiple parameters are separated using &.

Working with Query String

Node.js provides two main ways to work with query strings:

1. querystring Module

The querystring module provides utilities to parse and stringify query strings.

Importing Module: You can include the module using the following code:

const querystring = require('querystring');

Note: The querystring module is built-in in Node.js and does not require installation.

Example 1: Parsing a Query String (parse())

Output:

This is parsed Query String : { user: 'GEEKSFORGEEKS', year: '2021' }
  • Converts query string into a JavaScript object.
  • Values are returned as strings.

If the server is running and you open the following URL in the browser:

http://localhost:3000?id=2

Output:

{
"user": {
"id": 2,
"name": "James"
}
}
  • Extracts the query string from the URL and parses it using querystring.parse()
  • Uses the id query parameter to find the matching user from the array.
  • Returns the filtered user data as a JSON response.

Example 2: Converting Object to Query String (stringify())

Output:

name=Testing&company=GeeksforGeeks&content=Article&date=9thMarch2021
  • Useful for dynamically building URLs.
  • Automatically formats key=value pairs.

2. URL and URLSearchParams

URL and URLSearchParams are modern, built-in APIs in Node.js used to easily parse, read, and manage query parameters from URLs with clean and standardized syntax.

Example:

Assumed Request URL:

http://localhost:8080?id=1&name=Nicol

Output:

1
Nicol
  • URL() parses the full request URL.
  • url.search extracts the query string (?id=1&name=Nicol).
  • URLSearchParams reads individual parameters using get().

Query Parameters in GET Requests

Query parameters allow a GET request to retrieve specific data from the server using values passed in the URL, without sending a request body.

  • GET Requests: Retrieve data using parameters in the URL; no request body is sent.
  • URL Search Params: Key-value pairs added after ? (e.g., ?id=1&name=Nicol).
  • Node.js Usage: URLSearchParams is used to extract query parameters from the URL.

Benefits of Query Strings

Query strings provide a simple and efficient way to send small amounts of data to the server through the URL without using a request body.

  • Easy to pass data in GET requests and useful for filtering, searching, and sorting data
  • Improves URL readability and shareability
  • Enables better caching and SEO-friendly URLs
  • No need to modify server-side data or request body
Comment
Article Tags:

Explore