nodejs
  1. nodejs-querystring

Node.js Querystring

In Node.js, the querystring module provides utilities for working with query strings. Query strings are the part of the URL that come after the question mark (?), containing key-value pairs separated by an ampersand (&). In this tutorial, we'll discuss how to use the querystring module in Node.js.

Syntax

The querystring module provides two methods for parsing and stringifying query strings:

// Parse a query string into an object
querystring.parse(str, sep, eq, options)

// Stringify an object into a query string
querystring.stringify(obj, sep, eq, options)

Where:

  • str: The query string to parse.
  • sep: The separator used to separate key-value pairs. Default is '&'.
  • eq: The separator used to separate keys and values. Default is '='.
  • options: An optional object containing serialization options.

Example

Let's say we have the following URL:

https://example.com/search?q=node.js&lang=en&sort=most-recent

We can use the querystring module to parse the query string into an object:

const querystring = require('querystring');

const url = 'https://example.com/search?q=node.js&lang=en&sort=most-recent';
const query = url.split('?')[1];
const queryParams = querystring.parse(query);

console.log(queryParams); 
// Output: { q: 'node.js', lang: 'en', sort: 'most-recent' }

We can also use the querystring module to stringify an object into a query string:

const params = { q: 'node.js', lang: 'en', sort: 'most-recent' };
const queryString = querystring.stringify(params);

console.log(queryString);
// Output: 'q=node.js&lang=en&sort=most-recent'

Output

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

{ q: 'node.js', lang: 'en', sort: 'most-recent' }
q=node.js&lang=en&sort=most-recent

This is because we parsed the query string into an object and stringified an object into a query string, respectively.

Explanation

In the example above, we used the querystring module to parse a query string into an object and stringify an object into a query string. To parse the query string, we first split the URL at the '?' character and took the second part (the query string), and then we used querystring.parse() to parse the query string into an object. To stringify an object into a query string, we used querystring.stringify() and passed an object containing the query parameters.

Use

The querystring module is used to parse and stringify query strings in Node.js. It allows you to convert a query string into an object and vice versa.

Important Points

  • The querystring module is part of core Node.js, so you don't need to install it separately.
  • By default, querystring.parse() and querystring.stringify() use '&' and '=' as separators for key-value pairs. You can change these separators by passing them as options.
  • Values are always strings in querystring.parse(), so you may need to convert them to other types if needed.

Summary

In this tutorial, we discussed how to use the querystring module in Node.js. We covered the syntax, example, output, explanation, use, and important points of the querystring module. With this knowledge, you can now work with query strings in Node.js and parse them into objects and vice versa.

Published on: