The Wayback Machine - https://web.archive.org/web/20241126180150/https://www.geeksforgeeks.org/node-js-querystring-encode-function/
Open In App

Node.js querystring.encode() Function

Last Updated : 08 Mar, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The querystring.encode() method is used to produce a URL query string from the given object that contains the key-value pairs. The method iterates through the object’s own properties to generate the query string.

It can serialize a single or an array of strings, numbers, and boolean. Any other types of values are coerced to empty strings.

During serializing, the UTF-8 encoding format is used to encode any character that requires percent-encoding. To encode using an alternative character encoding, the encodeURIComponent option has to be specified.

Syntax:

querystring.encode( obj, sep, eq, options )

Parameters: This function accepts four parameters as mentioned above and described below:

  • obj: Object that has to be serialized into the URL query string.
  • sep: String that specifies the substring used to delimit the key and value pairs in the query string. The default value is “&”.
  • eq: String that specifies the substring used to delimit keys and values in the query string. The default value is “=”.
  • options: It is an Object which can be used to modify the behavior of the method. It has the following parameters:
    • encodeURIComponent: It is a function that would be used to convert URL-unsafe characters to percent-encoding in the query string. The default value is querystring.escape().

Return Value: It returns a String that contains the URL query produced from the given object.

Example 1:

Javascript




const querystring = require('querystring');
  
let obj = {
    user: "pratik", 
    isMale: true, 
    role: ["admin", "editor", "manager"], 
}
  
let output = querystring.encode(obj);
  
console.log("Output: ", output);


Output:

Image

Example 2:

Javascript




const querystring = require('querystring');
  
let obj = {
    user: "pratik", 
    isMale: true, 
    role: ["admin", "editor", "manager"], 
}
  
let output = querystring.encode(obj, '/', '->');
  
console.log("Output: ", output);


Output:

Image

Reference:https://nodejs.org/api/querystring.html#querystring_querystring_encode



Similar Reads

Node.js querystring.decode() Function
The querystring.decode() method is used to parse a URL query string into an object that contains the key and pair values of the query URL. The object returned does not inherit prototypes from the JavaScript object, therefore usual Object methods will not work. During parsing, the UTF-8 encoding format is assumed unless there is an alternative chara
2 min read
Node.js querystring.stringify() Method
The querystring.stringify() method is used to produce an URL query string from the given object that contains the key-value pairs. The method iterates through the object's own properties to generate the query string. It can serialize a single or an array of strings, numbers, and booleans. Any other types of values are coerced to empty strings. Duri
2 min read
Node.js querystring.parse() Method
The querystring.parse() method is used to parse a URL query string into an object that contains the key and pair values of the query URL. The object returned does not inherit prototypes from the JavaScript object, therefore usual Object methods will not work. During parsing, the UTF-8 encoding format is assumed unless there is an alternative charac
3 min read
Node.js querystring.escape() Method
The querystring.escape( ) function is used to produce a percent-encoded query string from a normal string. This method is very similar to the browser's encodeURIComponent functions. This method performs percent-encoding on the given string it means it encodes any string into a URL query string by using the % symbol. This method iterates through the
2 min read
Node.js querystring.unescape() Method
On the specified str, the querystring.unescape() method decodes URL percent-encoded characters. This method converts a percent-encoding string into a normal string. It means it decodes any percent-encoding string into a normal string by removing the % symbol. This method iterates through the string and removes the % encoding where needed on the spe
2 min read
ASP Request.QueryString Collection
The ASP Request.QueryString Collection is used to get the value of the variable that will be stored in the HTTP query string from the URL. The Query string is a part of the URL that begins with a question mark (?). Generally, it contains the information of the user. <a href= "geek.asp?txt=this is a query string test">Link with a query string
1 min read
How to encode/decode URL using AngularJS ?
In this article, we will see the mechanism to encode/decode URL using AngularJS, along with knowing the different methods available to accomplish the given task, & will understand it through the illustration. A URL specifies a resource and its access protocol. Encode URL: URL Encoding is a way to translate unprintable or reserved characters in
3 min read
Web TextEncoder API | TextEncoder encode() method
In HTML there is a TextEncoder Interface which has a method encode() which returns an integer array containing the input parameter in encoded form. Syntax: var str = encoder.encode( str ); Return: It return an Uint8array that contains the text which is given in parameters encoded with the specific method for that TextEncoder object. Example: This e
1 min read
Which functions are used to encode and decode JSON file in PHP ?
JSON stands for JavaScript Object Notation. Like XML, it is a text-based format for the exchange of data which is easier to read and write and it is lighter than other formats. JSON is based on two basic structures namely Objects and Arrays. Parsing JSON data in PHP: There are built-in functions in PHP for both encoding and decoding JSON data. Thes
2 min read
How to Encode and Decode URLs in PHP ?
In PHP, encoding and decoding URLs is a common task when working with web applications, especially when handling user input or constructing URLs dynamically. URL encoding is necessary to ensure that special characters in URLs are properly represented, while URL decoding is used to retrieve the original data from encoded URLs. Syntax// Encode a URL$
2 min read
How to Encode and Decode a URL in PHP?
Encoding and decoding URLs in PHP are essential tasks when working with web applications, especially for handling user input and constructing query strings. URL encoding ensures that special characters are properly represented in URLs, preventing errors or unexpected behavior during data transmission. PHP provides built-in functions to encode and d
2 min read
How to Encode Array in JSON PHP ?
Encoding arrays into JSON format is a common task in PHP, especially when building APIs or handling AJAX requests. Below are the approaches to encode arrays into JSON using PHP: Table of Content Using json_encode()Encoding Associative ArraysCustom JSON SerializationUsing json_encode()PHP provides a built-in function json_encode() to encode PHP data
2 min read
How to Encode URI in TypeScript?
When you are working with web applications in TypeScript, you often require encoding Uniform Resource Identifiers (URIs) in order to make sure that special characters are transmitted correctly across the internet. TypeScript which is a superset of JavaScript, gives you access to native JavaScript functions for URI encoding. Here are some common app
3 min read
JavaScript | Encode/Decode a string to Base64
To encode or decode strings in JavaScript, we can use the built-in functions provided by the language. These functions help in encoding special characters in a URL or decoding encoded strings back to their original form. 1. btoa() MethodThis method encodes a string in base-64 and uses the "A-Z", "a-z", "0-9", "+", "/" and "=" characters to encode t
6 min read
How to Encode and Decode a URL in JavaScript?
Encoding and decoding URLs in JavaScript is essential for web development, especially when making GET requests with query parameters. This process ensures special characters in URLs are correctly interpreted by the server. For instance, spaces are converted to %20 or + in URLs. This guide covers how to use JavaScript functions like encodeURI(), enc
5 min read
What are the advantages of synchronous function over asynchronous function in Node.js ?
Node.js furnishes us with an inbuilt fs (File System) module for different documents taking care of tasks like reading a record, composing a document, erasing a document, and so on. fs module can be introduced utilizing the underneath explanation: Syntax: npm introduce fs --save Note: The npm in the above order represents the hub bundle supervisor
5 min read
Node.js Automatic restart Node.js server with nodemon
We generally type following command for starting NodeJs server: node server.js In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again. node server.js It is a very hectic task for the development process. Nodemon is a package for handling this rest
1 min read
Top 3 Best Packages Of Node.js that you should try being a Node.js Developer
Node.js is an open-source and server-side platform built on Google Chrome's JavaScript Engine (V8 Engine). Node.js has its own package manager called NPM( Node Package Manager) which has very useful and incredible libraries and frameworks that makes our life easier as a developer to work with Node.js. The 3 Best Packages of Node.js that you should
4 min read
Javascript Program For Swapping Kth Node From Beginning With Kth Node From End In A Linked List
Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details line Name, RollNo, Address, ..etc). The pointers are always fixed (4 bytes for most
5 min read
Javascript Program For Inserting A Node After The N-th Node From The End
Insert a node x after the nth node from the end in the given singly linked list. It is guaranteed that the list contains the nth node from the end. Also 1 <= n. Examples: Input : list: 1->3->4->5 n = 4, x = 2 Output : 1->2->3->4->5 4th node from the end is 1 and insertion has been done after this node. Input : list: 10->8
5 min read
How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
Encountering the error message ‘node’ is not recognized as an internal or external command after installing Node.js can be frustrating, especially when you’re eager to start working on your project. This error usually means that your system cannot find the Node.js executable because the path to it is not set correctly in your system's environment v
3 min read
Node.js Connect Mysql with Node app
Node.js is a powerful platform for building server-side applications, and MySQL is a widely used relational database. Connecting these two can enable developers to build robust, data-driven applications. In this article, we'll explore how to connect a Node.js application with a MySQL database, covering the necessary setup, configuration, and basic
2 min read
Node.js slice() function
The slice() function is a string function of Node.js which is used to extract sub-string from a string. Syntax: string.slice( start, end ) Parameters: This function uses three parameters as mentioned above and described below: string: It holds the string content. The substring is extracted from this string.start: This parameter holds the starting i
1 min read
Node.js crypto.scryptSync( ) Function
The crypto.scryptSync() is an inbuilt function which Provides a synchronous scrypt implementation in Node.js. scrypt is a password-based key derivation function. It is intended to be costly computationally plus memory-wise. So, the brute-force attacks are made unsuccessful. Syntax: crypto.scryptSync(password, salt, keylen[, options]) Parameter: Thi
3 min read
Node.js console.log() Function
The console.log() function from console class of Node.js is used to display the messages on the console. It prints to stdout with newline. Syntax: console.log( [data][, ...] ) Parameter: This function contains multiple parameters which are to be printed. Return type: The function returns the passed parameters. Below program demonstrates the working
1 min read
Node.js indexOf() function
The indexOf() function is a string function from Node.js which is used to find a string with another string. Syntax: str1.indexOf( str2 ) Parameter: This function uses two parameters as mentioned above and described below: str1: It holds the string content where another string is to be searched.str2: It holds the search string. Return Value: The fu
1 min read
Node.js trim() function
The trim() function is a string function of Node.js which is used to remove white spaces from the string. Syntax: string.trim() Parameter: This function does not accept any parameter. Return Value: The function returns the string without white spaces. The below programs demonstrate the working of trim() function: Program 1: C/C++ Code function trim
1 min read
Node.js substr() function
The substr() function is a string function of Node.js which is used to extract sub-string from the given string. Syntax: string.substr(index, length) Parameters: This function accepts two parameters as mentioned above and described below: index: This parameter holds the starting index of the specified sub-string.length: This parameter holds the len
1 min read
Node.js console.warn() Function
The console.warn() function from console class of Node.js is used to display the warning messages on the console. It prints to stderr with newline. Note: This function is an alias of console.error() function. Syntax: console.warn( [data][, ...args] ) Parameter: This function can contains multiple parameters. The first parameter is used for the prim
1 min read
Node.js console.error() Function
The console.error() function from the console class of Node.js is used to display an error message on the console. It prints to stderr with a newline. Syntax: console.error([data][, ...args]) Parameter: This function can contain multiple parameters. The first parameter is used for the primary message and other parameters are used for substitution v
1 min read
three90RightbarBannerImg