天天看點

express接收前端post請求資料

開發wifi子產品配置時,遇到post資料在後端無論用req.body還是用req.params都無法獲得前端post過來的資料,經過baidu、google得到解決辦法

前端post過來的資料是以 Request Payload 格式傳給伺服器,

express接收前端post請求資料

這種格式資料是以流的形式傳遞給後端,此外以流的形式傳遞資料給後端還有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");    
        
    });
});​
           

  

轉載于:https://www.cnblogs.com/qingranEvent/p/7161784.html

繼續閱讀