- 原生 split
let str = req.url.split('?')[1];
let ary = str.split('&');
let obj = {};
for (let i = 0; i < ary.length; i++) {
let ary1 = ary[i].split('=');
obj[ary1[0]] = ary[1];
}
console.log(obj)
- nodjs->querystring把键值对的字符串转化为键值对的对象
const querystring=require('querystring');
let str = req.url.split('?')[1];
let obj=querystring.parse(str);
console.log(obj);
- nodejs->query
const url=require('url');
let obj=url.parse(req.url,true); //true将字符串转化为对象
obj=obj.query;
//简写
//let obj=url.parse(req.url,true).query;
console.log(obj);
网友评论