3.get请求获取数据方式

  1. 原生 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)
  1. nodjs->querystring把键值对的字符串转化为键值对的对象
 const querystring=require('querystring');
  let str = req.url.split('?')[1];
  let obj=querystring.parse(str);
  console.log(obj);
  1. 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);

你可能感兴趣的:(3.get请求获取数据方式)