ITPub博客

首页 > IT职业 > IT职场 > 六:node——post请求

六:node——post请求

IT职场 作者:KingSunSha 时间:2021-09-09 09:51:49 0 删除 编辑

post请求的数据比get大的多,所以post是分段发送请求的
post方式接收数据用 res.on
举个栗子:

html>
    
    Title
    用户名:     密码:          
const http = require("http")const querystring = require("querystring")

http.createServer(function(req,res){    // post接收数据的方式是用 req.on
    // get方式直接 req.url

    var str = ''
    var i = 0
    // data 表示每当有一段数据发送的时候,就触发一次(一共会发生很多次)
    req.on('data',function(data){        // console.log(`第${i++}次发送数据`)
        str += data
    })    // 数据全部到达时,只发生一次
    req.on('end',function(){        var post = querystring.parse(str)        console.log(post)        console.log("ok")
    })
}).listen(3333)

结果:

{ user: 'LISI', pass: '123', text: '123' }ok

关于queryString模块

  • 作用:将post数据解析成json



作者:椰果粒
链接:https://www.jianshu.com/p/d89b7c4d6bfb


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1011/viewspace-2813630/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    78
  • 访问量
    496