• Node.js Video Tutorials

NodeJS - urlSearchParams.set() Method



The NodeJS urlSearchParams.set() method of URLSearchParams class is used to set the name-value pairs to the query string.

The URLSearchParams API provides methods that give access to read and write the query of a URL. This class is also available on the global object.

Syntax

Following is the syntax of the NodeJS URLSearchParams.set() method

URLSearchParams.set(name, value)

Parameters

This method accepts two parameters. The same are described below.

  • name: This specifies the name of the parameter to set.

  • value: This specifies the value of the parameter to set.

Return Value

This method returns undefined.

Example

If we pass values to the ‘name’ and ‘value’ parameters of the NodeJS urlSearchParams.set() method, it will set those values as a name-value pair to the query string.

In the following example, we are trying to set a few name-value pairs to an empty query string.

const url = require('node:url');

let params = new URLSearchParams('');
console.log('Query string: ' + params);
console.log('Setting name-value pairs to the query string.....');

params.set('txt', 10);
params.set('pdf', 20);
params.set('dcmnt', 30);
params.set('word', 40);

console.log("Query string: " + params.toString());

Output

On executing the above program, it will generate the following output

Query string: 
Setting name-value pairs to the query string.....
Query string: txt=10&pdf=20&dcmnt=30&word=40

Example

If there are any pre-existing name-value pairs in the query string are matching the name to be set by the NodeJS set() method, it will remove all those matching name-value pairs and sets the newly added pair.

In the following example, we are trying to add a name-value pair that matches the existing name-value pair in the query string.

const url = require('node:url');

let params = new URLSearchParams('txt=10&pdf=20&dcmnt=30&word=40');
console.log('Query string: ' + params);

console.log('Setting name-value pairs to the query string.....');

params.set('txt', 15);
console.log("Query string: " + params.toString());

Output

On executing the above program, it will generate the following output

Query string: txt=10&pdf=20&dcmnt=30&word=40
Setting name-value pairs to the query string.....
Query string: txt=15&pdf=20&dcmnt=30&word=40

Example

If the values to be set by the set() method contains special characters, they will be percent-encoded and will be set to the query string.

const url = require('node:url');

let params = new URLSearchParams('txt=10&pdf=20&dcmnt=30&word=40');
console.log('Query string: ' + params);

console.log('Setting name-value pairs to the query string.....');

params.set('t`x`t', 15);	
console.log("Query string: " + params.toString());

Output

On executing the above program, it will generate the following output

Query string: txt=10&pdf=20&dcmnt=30&word=40
Setting name-value pairs to the query string.....
Query string: txt=10&pdf=20&dcmnt=30&word=40&t%60x%60t=15
nodejs_url_module.htm
Advertisements