Node.js Express raw data acquisition request

app.use (bodyParser.json ());
the client requests the interface if the request header named the Type-type = file application to the Content / json
bodyParser body automatically in the correct parse json format data,

// bodyParser raw-body dependent libraries, raw-body which has such a code library

function cleanup() {
   received = buffer = null
   stream.removeListener('data', onData)
   stream.removeListener('end', onEnd)
   stream.removeListener('error', onEnd)
   stream.removeListener('close', cleanup)
}

  


This results in express req listening on the inside, end event will not be implemented. To get the original data in the express request of the inside

Before you can re-register bodyPaser first save raw data up code is as follows:

app.use(function(req, res, next){
    var reqData = [];
    var size = 0;
    req.on('data', function (data) {
        console.log('>>>req on');
       reqData.push(data);
        size += data.length;
    });
    req.on('end', function () {
        req.reqData = Buffer.concat(reqData, size);
    });
    next();
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

  

 

Reproduced in: https: //www.cnblogs.com/hubcarl/p/4066183.html

Guess you like

Origin blog.csdn.net/weixin_34194317/article/details/93817351