简体   繁体   中英

req.on is not getting trigger while using type application/json

  req.on("data", (chunk) => {
  console.log("daataa second",body)
  body += chunk.toString();
});

When i am posting the data from the postman as the type then it successfully processing the data but when i am passing the data in json format then it is not triggering this method.

If i use json middleware, then none of the data or end event on request object is fired but I removed the json parser from your code, and then the events started firing

JSON parser middleware reads the entire request stream containing a JSON payload, parses it and creates a JavaScript object for you. For example in Express, express.json returns middleware that reads and saves JSON payloads as the req.body object if the request contains JSON data

JSON middleware needs to read the entire request stream in order to parse its content using JSON.parse(string) , and so will not call next to invoke further steps in request routing before the stream has been read and parsed. Hence listeners added later in the route to monitor data and/or end events will not fire because the data has already been read, and the end event has already fired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM