Home > Web Front-end > JS Tutorial > Node.js query string detailed explanation

Node.js query string detailed explanation

小云云
Release: 2018-02-01 09:16:26
Original
1534 people have browsed it

The query string module provides an API for parsing and formatting tool URL query strings. This article will introduce in detail the relevant content of Node.js query string parsing querystring, and share it for your reference and study. I won’t say much below. Okay, let’s take a look at the detailed introduction.

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

This API parses the URL query string into a collection of keys and values

  • str: ​​URL query string

  • sep: Indicates the symbol used to divide key-value pairs in the string, the default is "&"

  • eq: used to indicate the symbol between the key and value in the query string, the default is "="

The example is as follows:

const querystring = require('querystring');
const url = require('url');
var queryUrl="http://localhost:8888/bb?name=李浩&memo=helloworld&memo=helloC";
queryUrl=url.parse(queryUrl).query;
console.log(querystring.parse(queryUrl)) ;
----------------------------------------------------
// 输出 { name: '李浩', memo: [ 'helloworld', 'helloC' ] }
Copy after login

Example with parameters:

const querystring = require('querystring');
const url = require('url');
var queryUrl="http://localhost:8888/bb?name==李浩*memo==helloworld*memo==helloC";
queryUrl=url.parse(queryUrl).query;
console.log(querystring.parse(queryUrl,'*','==')) ; 
----------------------------------------------------
// 输出 { name: '李浩', memo: [ 'helloworld', 'helloC' ] }
Copy after login

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

This API is generated from a given object A URL query string, by traversing the object's "own properties".

  • obj is the given object

  • sep is the separator between key-value pairs, the default is "&"

  • eq is the connection symbol between key and value, the default is "="

The example is as follows:

const qs=require("querystring");
var obj={ name: '李浩', memo: [ 'helloworld', 'helloC' ] }
console.log(
 qs.stringify(obj)
)
/* 
 输出 name=%E6%9D%8E%E6%B5%A9&memo=helloworld&memo=helloC;
 API默认是对中文进行了编码,下面我们再说编码解码的API
*/
Copy after login

Example with parameters :

const qs=require("querystring");
var obj={ name: '李浩', memo: [ 'helloworld', 'helloC' ] }
console.log(
 qs.stringify(obj,'@','==')
)
Copy after login

Output name==%E6%9D%8E%E6%B5%A9@memo==helloworld@memo==helloC;

I believe you should see the difference between with parameters and without Is there a difference with parameters?

3. querystring.unescape(str)

This API decodes the URL in the URL query string. For example, we perform the stringify operation above. , the Chinese will be encoded, and the Chinese will also be parsed into the encoded form when it is in the URL. This API is generally used in conjunction with querystring.parse()

Let’s take the encoding of the Chinese above Examples of URL query strings

The examples are as follows:

const qs = require("querystring");
var url = "name=%E6%9D%8E%E6%B5%A9&memo=helloworld&memo=helloC"
var normalStr = qs.unescape(url)
console.log(
 qs.parse(normalStr)
)
//输出 { name: '李浩', memo: [ 'helloworld', 'helloC' ] }
Copy after login

4. querystring.escape(str)

This API is equivalent to the reverse operation of querystring.unescape(str) Now, to encode Chinese, often use it together with querystring.stringify()

var obj = { name: '李浩', memo: ['helloworld', 'helloC'] };
console.log(
 qs.escape(
  qs.stringify(obj)
 )
)
// 输出 name%3D%25E6%259D%258E%25E6%25B5%25A9%26memo%3Dhelloworld%26memo%3DhelloC
//连同&和=一起编码了
Copy after login

Related recommendations:

Share the introduction to queryString in NodeJS

Summary of methods to obtain QueryString using js_javascript skills

Instructions for using the querystring.parse method in node.js_node.js

The above is the detailed content of Node.js query string detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template