express接收前端post请求数据

开发wifi模块配置时,遇到post数据在后端无论用req.body还是用req.params都无法获得前端post过来的数据,经过baidu、google得到解决办法

前端post过来的数据是以 Request Payload 格式传给服务器,

这种格式数据是以流的形式传递给后端,此外以流的形式传递数据给后端还有post提交文件时的 Form Data格式,

对于流模式传输数据,node服务器应监听req的data事件来接受数据

router.use('/',function (req, res, next) {    
    var str = "";    
    req.on("data",function (chunk) {     
        str += chunk;    
        
    });    
    req.on("end",function () { 
        console.log(str);   
        res.end("ok");    
        
    });
});​

  

posted @ 2017-07-13 17:44  qingranEvent  阅读(2184)  评论(1编辑  收藏  举报