App.use (Bodyparser.json ());
When a client requests an interface, if the named request header type is Content-type=application/json
The Bodyparser will automatically parse the JSON format data in the body correctly,
Bodyparser relies on Raw-body library, Raw-body Library has such a piece of code
function Cleanup () { received = buffer = null stream.removelistener (' data ', OnData) Stream.removelistener (' End ', onEnd) Stream.removelistener (' Error ', onEnd) stream.removelistener (' close ', Cleanup)}
This results in the Req listening on in Express, and the end event is not executed. To get the original request data in Express
You can save the original data before registering Bodypaser. The 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 ());
node. js Express GET request raw data