Query Strings in Node.js

The querystring module in Node.js provides utilities for parsing and formatting query strings. It allows to convert query strings to objects and vice versa, as well as to escape and unescape special characters in URLs.


1. querystring.parse()

import * as querystring from 'querystring';

const query = 'name=John&Doe&age=30';
const parsed = querystring.parse(query);

console.log(parsed); 
// Output: { name: 'John', 'Doe': '', age: '30' }

2. querystring.stringify()

import * as querystring from 'querystring';

const obj = { name: 'John', age: 30 };
const str = querystring.stringify(obj);

console.log(str); 
// Output: name=John&age=30

3. querystring.escape()

import * as querystring from 'querystring';

const escaped = querystring.escape('Hello World!');

console.log(escaped); 
// Output: Hello%20World%21

4. querystring.unescape()

import * as querystring from 'querystring';

const unescaped = querystring.unescape('Hello%20World%21');

console.log(unescaped); 
// Output: Hello World!