必须end后才能收到post
在用原生node写后台的时候,如果最后没有end()的话发现就算执行到获取post的语句但是任然没有获得post数据
console.log(postmsg)
//收到请求
///api/login
//[Object: null prototype] {}
//[Object: null prototype] {}
在使用end后:
console.log(postmsg)
res.end()
// [Object: null prototype] { name: '23', password: '23' }
///[Object: null prototype] { name: '23', password: '23' }
原因分析:
估计是只要没有end发出,on(‘end’) 方法就一直判定为还没有完成所以就一直没有执行on(‘end’)事件:
let sy=new Promise(function(resolve,reject){
req.on('data',function(chunk){
post=post+chunk
})
req.on('end',function(){
post=post.replace(/{/g,'')
post=post.replace(/"/g,'')
post=post.replace(/'/g,'')
post=post.replace(/}/g,'')
post=QurryString.parse(post,',',':')
resolve(post)
})
}).then(function(postmsg){
post=postmsg
return Promise.resolve(postmsg);
})