node初探——表单提交(get方式)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<form action="http://localhost:8080/aaa" methods="get">
  用户:<input type="text" name="user" value=""/><br/>
  密码:<input type="password" name="pass" value=""/><br/>
  <input type="submit" name="name" value="提交"/>
</form>
</body>
</html>
const http=require('http');
var server=http.createServer(function(req,res){
    
    
  // console.log(req.url);
  var GET={
    
    };
  if(req.url.indexOf('?')!=-1){
    
    
    var arr=req.url.split('?');
    //arr[0]=>地址 '/aaa'
    var url=arr[0];

    // res.write("aaa");
    // res.end();
    var arr2=arr[1].split('&');
    //arr2=>['user=blue','pass=123'
    for(var i=0;i<arr2.length;i++){
    
    
      var arr3=arr2[i].split('=');
      //arr2[0]=>名字 'user'
      //arr2[1]=>数据 'blue'
      GET[arr3[0]]=arr3[1];
    }
  }else{
    
    
    var url=req.url;
  }
  console.log(url,GET);
}).listen(8080);

猜你喜欢

转载自blog.csdn.net/tozeroblog/article/details/85218540
今日推荐