Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Node.js querystring Module

Node.js querystring Module

Java FileNode.js Built-in Modules


querystring is a built-in core module of Node.js, dedicated to parsing and formatting URL query strings. A query string is the part of a URL after the question mark (?) and usually consists of key-value pairs, for example:?name=John&age=30。

This module is very practical in scenarios such as front-end/back-end data transmission and API development, helping developers easily handle URL parameters.


Core Method Analysis

querystring.parse(str[, sep[, eq[, options]]])

Parses a query string into a JavaScript object.

Parameter Description:

- `maxKeys`: specifies the maximum number of keys to parse, default is 1000 - `decodeURIComponent`: the function used for decoding, default is `querystring.unescape()`

Example code:

Example

const

querystring

=

require

(

'querystring'

)

;

const

query

=

'name=John&age=30&city=New+York'

;

const

parsed

=

querystring.

parse

(

query

)

;

console.

log

(

parsed

)

;

// Output: { name: 'John', age: '30', city: 'New York' }


querystring.stringify(obj[, sep[, eq[, options]]])

Serializes a JavaScript object into a query string.

Parameter Description:

- `encodeURIComponent`: the function used for encoding, default is `querystring.escape()`

Example code:

Example

const

querystring

=

require

(

'querystring'

)

;

const

obj

=

{

  name

:

'John Doe'

,

  age

:

25

,

  occupation

:

'Software Developer'

}

;

const

stringified

=

querystring.

stringify

(

obj

)

;

console.

log

(

stringified

)

;

// Output: 'name=John%20Doe&age=25&occupation=Software%20Developer'


querystring.escape(str) and querystring.unescape(str)

These two methods are used to encode and decode special characters in a query string, respectively.

Example code:

Example

const

querystring

=

require

(

'querystring'

)

;

const

str

=

'Hello World!'

;

const

escaped

=

querystring.

escape

(

str

)

;

console.

log

(

escaped

)

;

// Output: 'Hello%20World%21'

const

unescaped

=

querystring.

unescape

(

escaped

)

;

console.

log

(

unescaped

)

;

// Output: 'Hello World!'


Practical Application Scenarios

1. Handling GET request parameters

Example

const

http

=

require

(

'http'

)

;

const

querystring

=

require

(

'querystring'

)

;

http.

createServer

(

(

req

,

res

)

=>

{

 

if

(

req.

url

.

includes

(

'?'

)

)

{

   

const

queryString

=

req.

url

.

split

(

'?'

)

[

1

]

;

   

const

params

=

querystring.

parse

(

queryString

)

;

   

    console.

log

(

params

)

;

    res.

end

(

`Received parameters

:

$

{

JSON.

stringify

(

params

)

}

`

)

;

 

}

else

{

    res.

end

(

'No query parameters received'

)

;

 

}

}

)

.

listen

(

3000

)

;

2. Building API query strings

Example

const

querystring

=

require

(

'querystring'

)

;

const

apiParams

=

{

  q

:

'node.js'

,

  page

:

1

,

  limit

:

10

,

  sort

:

'desc'

}

;

const

apiUrl

=

`https

:

//api.example.com/search?${querystring.stringify(apiParams)}`;

console.

log

(

apiUrl

)

;

// Output: 'https://api.example.com/search?q=node.js&page=1&limit=10&sort=desc'


Notes

  1. Encoding issues: By default, querystring encodes/decodes special characters. If you need custom encoding, you can pass in encodeURIComponent or decodeURIComponent function in Vue 3.

  2. Data Type: All parsed values are of string type and need to be manually converted to other types (such as numbers, booleans, etc.).

  3. Nested objects: The querystring module does not support parsing and serialization of nested objects. If you need to handle complex data structures, consider using JSON format.

  4. URL-safe characters: spaces are encoded as + instead of %20, which is different from the encodeURIComponent behavior.


Alternatives

Although the querystring module is practical, newer versions of Node.js recommend using the URL and URLSearchParams APIs, which provide more modern and comprehensive URL handling capabilities.

Example

// Example using URLSearchParams

const

params

=

new

URLSearchParams

(

'name=John&age=30'

)

;

console.

log

(

params.

get

(

'name'

)

)

;

// Output: 'John'


Summary

The querystring module is a great tool for handling query strings in Node.js, especially for simple key-value pair data. Mastering its core methods (parse, stringify, escape, unescape) can greatly improve development efficiency. For more complex URL handling needs, consider using the URL and URLSearchParams APIs.

Java FileNode.js Built-in Modules