nodejs
  1. nodejs-query-string

Node.js Query String

In Node.js, the querystring module provides methods for parsing and formatting URL query strings. The query string is a string representation of key-value pairs that are sent as part of the URL, typically for GET requests. This module can be useful when working with APIs that expect URL query strings as input or output.

Syntax

The syntax for parsing a query string in Node.js is as follows:

const querystring = require('querystring');
const parsed = querystring.parse('key1=value1&key2=value2');
console.log(parsed); // Output: { key1: 'value1', key2: 'value2' }

The syntax for formatting a query string in Node.js is as follows:

const querystring = require('querystring');
const formatted = querystring.stringify({ key1: 'value1', key2: 'value2' });
console.log(formatted); // Output: 'key1=value1&key2=value2'

Example

Let's say we want to parse a query string containing several key-value pairs and log the results to the console. Here's how we can implement it:

const querystring = require('querystring');
const parsed = querystring.parse('name=John&age=25&city=Paris');
console.log(parsed); // Output: { name: 'John', age: '25', city: 'Paris' }

Now, let's say we want to format an object containing several key-value pairs into a query string. Here's how we can implement it:

const querystring = require('querystring');
const formatted = querystring.stringify({ name: 'John', age: 25, city: 'Paris' });
console.log(formatted); // Output: 'name=John&age=25&city=Paris'

Output

When we run the example code above, the output will be:

{ name: 'John', age: '25', city: 'Paris' }
name=John&age=25&city=Paris

This is because we first parsed a query string containing several key-value pairs and logged the results to the console. We then formatted an object containing several key-value pairs into a query string and logged the result to the console.

Explanation

In the examples above, we used the querystring module to parse and format URL query strings. We parsed a query string containing several key-value pairs into an object using the querystring.parse() method, and then formatted an object containing several key-value pairs into a query string using the querystring.stringify() method.

Use

The querystring module can be useful when working with APIs that expect URL query strings as input or output. You can use it to parse query strings into objects, or to format objects into query strings.

Important Points

  • When parsing a query string, the values are always returned as strings.
  • When formatting an object into a query string, any values that are not strings will be converted to strings using their toString() methods.
  • You can also use the querystring.escape() and querystring.unescape() methods to escape and unescape characters in a URL-encoded string.

Summary

In this tutorial, we discussed how to use the querystring module in Node.js. We covered the syntax, examples, output, explanation, use, and important points of the querystring module. With this knowledge, you can now use the querystring module in your Node.js applications to parse and format URL query strings.

Published on: