亲宝软件园·资讯

展开

node get数据解析 怎样在node环境实现“get数据解析”代码实例

奔跑的太阳花 人气:0

1、自己写

const http = require('http');
 
http.createServer(function(req,res){
  var get = {};
  if(req.url.indexOf('?')!=-1){
    var arr = req.url.split('?');
    var url = arr[0];
    var arr2 = arr[1].split('&');
    for(var i=0;i<arr2.length;i++){
      var arr3 = arr2[i].split('=');
      get[arr3[0]]=arr3[1];
    }
  }else{
    var url = req.url;
  }
  console.log(url,get);
  res.write('aaa');
  res.end();
}).listen(8080);

2、引入querystring模块

const http = require('http');
const querystring = require('querystring');
http.createServer(function(req,res){
  var get = {};
  if(req.url.indexOf('?')!=-1){
    var arr = req.url.split('?');
    var url = arr[0];
    get = querystring.parse(arr[1]);
  }else{
    var url = req.url;
  }
  console.log(url,get);
  res.write('aaa');
  res.end();
}).listen(8080);

3、引入url模块

const http = require('http');
const urlli = require('url');
http.createServer(function(req,res){
  var obj = urlli.parse(req.url,true); //true解析,如果没有true,默认不解析
  var url = obj.pathname;
  var get = obj.query;
  console.log(url,get);
  res.write('aaa');
  res.end();
}).listen(8080);

提交表单的HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <form action="http://localhost:8080/aaa" method="get">
    用户:<input type="text" name="user" value=''><br>
    密码:<input type="password" name="pass" value=''><br>
    <input type="submit" value="提交">
  </form>
</body>
</html>

个人练习如下:

const http = require("http");
const queryString = require("querystring");
const urlNode = require('url');

http.createServer((req,res)=>{
  console.log(req.url);  // /aaa?user=123&pass=456
  var get = {};
  var url = '';
  if(req.url.indexOf('?') != -1){

    //方法一:切割字符串
    // var _arr = req.url.split('?'); //['aaa','user=123&pass=456']
    // url = _arr[0]; //'aaa'
    // var _arr2 = _arr[1].split('&'); //['user=123','pass=456']
    // _arr2.map((v,k,arr)=>{
    //   var key = v.split('=')[0];
    //   var value = v.split('=')[1];
    //   get[key] = value;
    // })

    //方法二:正则表达式
    // var query = req.url.substr(req.url.indexOf('?')+1);
    // var regAll = new RegExp("([^?&=]+)=([^?&=]*)", "gi");
    // var regSingle = new RegExp("([^?&=]+)=([^?&=]*)", "i");
    // var arrAll = query.match(regAll);   //["user=123", "pass=456"]
    // arrAll.map(function(v,k,arr){
    //   var resultArr = v.match(regSingle);
    //   get[resultArr[1]] = resultArr[2];
    // })
    //方法三:引入querystring模块
    // const arr = req.url.split('?');
    // url = arr[0];
    // get = queryString.parse(arr[1]);
    //方法四:
    // const obj = urlNode.parse(req.url,true);
    // url = obj.pathname;
    // get = obj.query;

  }else{
    url = req.url;
  }
  console.log(url,get);
  res.write('123456')
  res.end('12')
}).listen(8000)

以上都 是原生node实现:

下面的是express中 req.query 就可以方便的取到 { id 123456}

app.get('/article',(req,res)=>{
  console.log(req.query)
  
})

加载全部内容

相关教程
猜你喜欢
用户评论