美文网首页
POST数据请求

POST数据请求

作者: 王伯卿 | 来源:发表于2017-12-30 19:58 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
    </head>
    <body>
      <form action="http://localhost:3000/user" method="post">
        username <input type="text" name="user" value=""><br>
        password <input type="password" name="password" value=""><br>
        <input type="submit" value="send">
      </form>
    </body>
    </html>
    
    const http = require("http");
    
    //GET数据的内容存在req.url中
    //POST数据在内容部分,我们form表单提交的内容,就是POST数据
    var server = http.createServer(function(req , res){
      //先声明一个空的字符串用来接收POST数据
      //不过这样不是好方法,因为这样没办法处理图片音频,暂时先这样
      var str = "";
      //data事件,用于接收POST数据,每一段POST数据过来的时候,都会发生一次
      req.on("data" , function(data){
        str += data;
      });
      //如果没有data数据后,end事件就会被触发
      req.on("end" , function(){
        console.log(str);
      });
    }).listen(3000 , function(err){
      if(!err){
        console.log("server is listening 3000 port");
      }else{
        console.log(err);
      }
    });
    

    提交form表单后,后台将会打印出

    user=abc&password=123
    

    而这个就可以用querystring模块来处理了。

    相关文章

      网友评论

          本文标题:POST数据请求

          本文链接:https://www.haomeiwen.com/subject/bxengxtx.html