Module basic module

module querystring

The querystring module provides some practical functions for parsing and serializing URL query parameters. Using the querystring module, you can easily parse URL query parameters into objects or strings, and you can also serialize objects into URL query parameter strings.

The following are querystringcommonly used functions of the module:

  • querystring.parse(str[, sep[, eq[, options]]]): Parse URL query parameters into objects
  • querystring.stringify(obj[, sep[, eq[, options]]]): Serialize the object into a URL query parameter string

Among them, stris the URL query parameter string to be parsed and objis the object to be serialized.

The following example shows how to use querystringthe module to parse query parameters from a URL into objects:

1 2 3 4 5 6 7 8
const querystring = require('querystring'); const url = 'https://www.example.com/path/to/page?foo=bar&baz=qux'; const search = new URL(url).search; // return '?foo=bar&baz=qux' const query = querystring.parse(search.slice(1)); // parse query string console.log(query); // output { foo: 'bar', baz: 'qux' }

The above code first obtains a URL, then extracts the query parameter part from it, uses querystring.parse()the function to parse it into an object, and finally prints the object.

Next, an example shows how to use querystringthe module to serialize an object into a URL query parameter string:

1 2 3 4 5 6 7 8 9 10
const querystring = require('querystring'); const obj = { foo: 'bar', baz: 'qux' }; const query = querystring.stringify(obj); console.log(query); // output "foo=bar&baz=qux"

In the above code, an object is first defined, then querystring.stringify()the function is used to serialize it into a URL query parameter string, and finally the string is printed out.

It can be found that using querystringthe module can easily parse and serialize URL query parameters, reducing the cumbersome processing of strings and improving the readability and maintainability of the code.

static function

escape

urlPart string security encoding

1
static String querystring.escape(String str);

Call parameters:

  • str: String, to be encodedurl

Return results:

  • String, returns the encoded string

unescape

urlSecure string decoding

1
static String querystring.unescape(String str);

Call parameters:

  • str: String, to be decodedurl

Return results:

  • String, returns the decoded string

parse

Parse query string

1 2 3 4
static HttpCollection querystring.parse(String str, String sep = "&", String eq = "=", Object opt = {});

Call parameters:

  • str: String, the string to be parsed
  • sep: String, the split string used during parsing, the default is &
  • eq: String, the assignment string used during parsing, the default is =
  • opt: Object, parsing parameters, not supported yet

Return results:


stringify

Serialize an object into a query string

1 2 3 4
static String querystring.stringify(Object obj, String sep = "&", String eq = "=", Object opt = {});

Call parameters:

  • obj: Object, the object to be serialized
  • sep: String, the split string used during serialization, the default is &
  • eq: String, the assignment string used during serialization, the default is =
  • opt: Object, parsing parameters, not supported yet

Return results:

  • String, returns the serialized string