node. js Express GET request raw data

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.