Use of qs library

The qs library converts the parameters in the form of x-www-form-urlencoded in the URL into objects or strings.

npm i qs

Qs.parse(to be converted)

let Qs = require("qs")
let url = 'username=lilei&age=23&phone=18600001234'
let urlObj = Qs.parse(url)
console.log(urlObj)
/*
 {
 	username:'lilei',
 	age:'23',
 	phone:'18600001234'
 }
*/

qs is separated by equal signs and & symbols, if you add the agreement, etc., it will also be brought with the agreement

let Qs = require("qs")
let url = 'http://localhost:3000?username=lilei&age=23&phone=18600001234'
let urlObj = Qs.parse(url)
console.log(urlObj)
/*
{
  'http://localhost:3000?username': 'lilei',//将协议和域名等也带上了
  age: '23',
  phone: '18600001234'
}
*/

Qs.stringify(to_convert) Converts the object to x-www-form-urlencoded form.

const a = {
    
    name:'hehe',age:10};
// 转为url参数形式
console.log(qs.stringify(a))
// name=hehe&age=10JSON.stringify的区别是,将转换为JSON格式
console.log(JSON.stringify(a))
//{"name":"hehe","age":10}

Guess you like

Origin blog.csdn.net/qq_31676483/article/details/129200434