首页 > IT职业 > IT职场 > 六:node——post请求
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/ ,如需转载,请注明出处,否则将追究法律责任。