Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
All Practice Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Node.js Querystring Module Parse URL Params: Tutorial, Examples, FAQs & Interview Tips

What is the Querystring Module?

The Node.js querystring module provides utilities for parsing and formatting URL query strings. Query strings are the key-value pairs that appear after the ? in a URL (e.g., ?name=Alice&age=25). This module allows you to convert query strings into JavaScript objects and vice versa, making it easier to work with URL parameters in web applications.

Important Note: The querystring module is considered legacy as of Node.js 10+. For new projects, use the modern URLSearchParams API, which is part of the WHATWG URL standard and provides better functionality with a cleaner API.

Including the Querystring Module

Require Querystring Module

Require Querystring Module
// Legacy approach (querystring module)
const querystring = require('querystring');

// Modern approach (URLSearchParams - recommended)
const { URLSearchParams } = require('url');
// Or in Node.js 10+, URLSearchParams is global
const params = new URLSearchParams();

Querystring Module Methods

The querystring module provides four main methods for working with query strings:

Method Description Example
parse(str) Parses a URL query string into a key-value object parse('a=1&b=2') → {a:'1', b:'2'}
stringify(obj) Converts an object into a URL query string stringify({a:1, b:2}) → 'a=1&b=2'
escape(str) URL-encodes a string (percent-encoding) escape('hello world') → 'hello%20world'
unescape(str) URL-decodes a string unescape('hello%20world') → 'hello world'

1. querystring.parse() - Parse Query String to Object

The parse() method converts a query string into a JavaScript object. It automatically handles URL decoding and splits parameters by & and =.

querystring.parse() Examples

querystring.parse() Examples
const qs = require('querystring');

// Basic parsing
const parsed1 = qs.parse('name=Alice&age=25&city=Delhi');
console.log(parsed1);
// Output: { name: 'Alice', age: '25', city: 'Delhi' }

// Parsing with URL-encoded characters
const parsed2 = qs.parse('name=John%20Doe&email=john%40example.com');
console.log(parsed2);
// Output: { name: 'John Doe', email: 'john@example.com' }

// Parsing with multiple values for same key
const parsed3 = qs.parse('color=red&color=blue&color=green');
console.log(parsed3);
// Output: { color: ['red', 'blue', 'green'] }

// Custom separator and assignment operator
const parsed4 = qs.parse('name:Alice;age:25', ';', ':');
console.log(parsed4);
// Output: { name: 'Alice', age: '25' }

// Parsing from a full URL (extract query string first)
const url = 'https://example.com/search?q=nodejs&page=2&limit=10';
const queryString = url.split('?')[1];
const parsed5 = qs.parse(queryString);
console.log(parsed5);
// Output: { q: 'nodejs', page: '2', limit: '10' }

2. querystring.stringify() - Convert Object to Query String

The stringify() method converts a JavaScript object into a URL query string format. It automatically handles URL encoding for special characters.

querystring.stringify() Examples

querystring.stringify() Examples
const qs = require('querystring');

// Basic stringification
const obj1 = { name: 'Bob', role: 'admin', active: true };
const str1 = qs.stringify(obj1);
console.log(str1);
// Output: name=Bob&role=admin&active=true

// With special characters (auto URL-encoded)
const obj2 = { name: 'John Doe', email: 'john@example.com' };
const str2 = qs.stringify(obj2);
console.log(str2);
// Output: name=John%20Doe&email=john%40example.com

// With array values
const obj3 = { colors: ['red', 'blue', 'green'] };
const str3 = qs.stringify(obj3);
console.log(str3);
// Output: colors=red&colors=blue&colors=green

// Custom separator and assignment operator
const obj4 = { name: 'Alice', age: 25 };
const str4 = qs.stringify(obj4, ';', ':');
console.log(str4);
// Output: name:Alice;age:25

// Building a complete URL
const baseUrl = 'https://api.example.com/search';
const params = { q: 'node.js', page: 1, limit: 20 };
const fullUrl = `${baseUrl}?${qs.stringify(params)}`;
console.log(fullUrl);
// Output: https://api.example.com/search?q=node.js&page=1&limit=20

3. querystring.escape() and unescape()

These methods handle URL encoding and decoding. The escape() method converts special characters to percent-encoded format, while unescape() reverses the process.

Escape and Unescape Examples

Escape and Unescape Examples
const qs = require('querystring');

// Escape special characters
console.log(qs.escape('hello world'));        // hello%20world
console.log(qs.escape('user@example.com'));   // user%40example.com
console.log(qs.escape('a+b=c'));              // a%2Bb%3Dc
console.log(qs.escape('100% complete'));      // 100%25%20complete

// Unescape encoded strings
console.log(qs.unescape('hello%20world'));    // hello world
console.log(qs.unescape('user%40example.com')); // user@example.com
console.log(qs.unescape('a%2Bb%3Dc'));        // a+b=c

// Note: parse() and stringify() automatically call escape/unescape
const obj = { message: 'Hello World!' };
const encoded = qs.stringify(obj);
console.log(encoded);  // message=Hello%20World!
const decoded = qs.parse(encoded);
console.log(decoded);  // { message: 'Hello World!' }

Modern Alternative: URLSearchParams (Recommended)

The URLSearchParams API is the modern, standard way to work with query strings in Node.js 10+ and all modern browsers. It provides a cleaner, more intuitive API with additional features.

URLSearchParams - Modern Approach

URLSearchParams - Modern Approach
// URLSearchParams is globally available in Node.js 10+
const { URL, URLSearchParams } = require('url');

// Parse query string
const params1 = new URLSearchParams('name=Alice&age=25&city=Delhi');
console.log(params1.get('name'));   // Alice
console.log(params1.get('age'));    // 25
console.log(params1.has('city'));   // true

// Build query string from object
const params2 = new URLSearchParams({ name: 'Bob', page: 1 });
console.log(params2.toString());    // name=Bob&page=1

// Add, update, delete parameters
params2.append('limit', 20);        // Add new parameter
params2.set('page', 2);             // Update existing parameter
params2.delete('name');             // Remove parameter
console.log(params2.toString());    // page=2&limit=20

// Iterate over parameters
const params3 = new URLSearchParams('a=1&b=2&c=3');
for (const [key, value] of params3) {
    console.log(`${key}: ${value}`);
}
// Output: a: 1, b: 2, c: 3

// Use with URL module
const url = new URL('https://example.com/search?q=nodejs&page=2');
console.log(url.searchParams.get('q'));      // nodejs
console.log(url.searchParams.get('page'));   // 2

// Modify URL parameters
url.searchParams.set('page', 3);
url.searchParams.append('limit', 10);
console.log(url.href);
// https://example.com/search?q=nodejs&page=3&limit=10

// Convert to object (requires manual conversion)
const paramsObj = Object.fromEntries(url.searchParams);
console.log(paramsObj);
// { q: 'nodejs', page: '3', limit: '10' }

Practical Use Cases

Real-World Examples

Real-World Examples
const http = require('http');
const url = require('url');
const querystring = require('querystring');

// Example 1: Parse query parameters in HTTP server
const server = http.createServer((req, res) => {
    const parsedUrl = url.parse(req.url);
    const query = querystring.parse(parsedUrl.query);
    
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify({
        path: parsedUrl.pathname,
        params: query
    }));
});

server.listen(3000);
// Visit: http://localhost:3000/search?q=nodejs&page=2

// Example 2: Build API request URLs
function buildApiUrl(endpoint, params) {
    const baseUrl = 'https://api.example.com';
    const queryString = querystring.stringify(params);
    return `${baseUrl}${endpoint}?${queryString}`;
}

const apiUrl = buildApiUrl('/users', { 
    role: 'admin', 
    active: true, 
    limit: 50 
});
console.log(apiUrl);
// https://api.example.com/users?role=admin&active=true&limit=50

// Example 3: Parse form data (application/x-www-form-urlencoded)
const formData = 'username=alice&password=secret123&remember=on';
const parsed = querystring.parse(formData);
console.log(parsed);
// { username: 'alice', password: 'secret123', remember: 'on' }

Querystring vs URLSearchParams Comparison

Feature querystring URLSearchParams
Status Legacy (deprecated) Modern standard (recommended)
Availability Node.js built-in Node.js 10+ and browsers
Parse query string parse(str) new URLSearchParams(str)
Build query string stringify(obj) params.toString()
Get parameter Access object property params.get(key)
Set parameter Modify object params.set(key, value)
Iteration Use Object methods Built-in iterator support
Integration Standalone Works with URL module

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.