post相比get的优势
安全性更高
数据容量更大
post为什么分段传输数据
提高容错性
避免数据传输阻塞
与post相关的两种回调函数
段数据到达触发(多次)
req.on('data',function(data){});
所有数据到达触发(一次),get也具有此方法
req.on('end',function(){});
form表单,以post形式提交至http://localhost:8000/NP
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="http://localhost:8000/NP" method="post">
用户名:<input type="text" name="user" value=""><br/>
密码:<input type="password" name="pwd" value=""><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
post.js实现数据解析
var http=require('http');
var querystring=require('querystring');
http.createServer(function(req,res)
{
var num=0
var str=''
var POST={}
if(req.url!="/favicon.ico")
{
req.on('data',function(data){
num++
console.log(`第${num}次收到数据`)
str+=data
});
req.on('end',function(){
POST=querystring.parse(str)// str='user=xxx&&pwd=xxxxx'
console.log(POST)
});
res.end();
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');var http=require('http');
实现get和post的server.js
const http=require('http');
const urlib=require('url');
var querystring=require('querystring');
http.createServer(function(req,res)
{
var str=''
var POST={}
var GET={}
var url=''
if(req.url!="/favicon.ico")
{
req.on('data',function(data){
str+=data
});
req.on('end',function(){
POST=querystring.parse(str)
console.log(url,GET,POST)
});
var arr=urlib.parse(req.url,true)
url=arr.pathname
GET=arr.query
res.end();
}
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');