简体   繁体   English

使用类型 application/json 时 req.on 未触发

[英]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.当我将邮递员的数据作为类型发布时,它会成功处理数据,但是当我以 json 格式传递数据时,它不会触发此方法。

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 中间件,则不会触发请求对象上的任何数据或结束事件,但我从您的代码中删除了 json 解析器,然后事件开始触发

JSON parser middleware reads the entire request stream containing a JSON payload, parses it and creates a JavaScript object for you. JSON 解析器中间件读取包含 JSON 有效负载的整个请求流,对其进行解析并为您创建一个 JavaScript 对象。 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例如在 Express 中,如果请求包含 JSON 数据, express.json返回读取 JSON 有效负载并将其保存为req.body对象的中间件

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. JSON 中间件需要读取整个请求流才能使用JSON.parse(string)解析其内容,因此在读取和解析流之前不会调用next来调用请求路由中的进一步步骤。 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.因此,稍后在路由中添加的侦听器以监视data和/或end事件将不会触发,因为数据已经被读取,并且end事件已经触发。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 node.js~express req.on('end')触发但不是req.on('data') - node.js ~ express req.on('end') triggers but not req.on('data') 我的 req.on('end') 没有触发。 如何解决这个问题? - My req.on('end') does not fire. How can fix this? 平均值:上传的文件将完全忽略req.on()NodeJS方法 - MEAN: A file uploaded ignores completely the req.on() NodeJS method nodejs req.on('end', callback) 不起作用属性 - nodejs req.on('end', callback) doesn't work property Node.js Vanilla Routes Req.on 不发送响应 - Node.js Vanilla Routes Req.on not sending response 无法使用 express 和 mongoose 进行路由,不断收到“类型错误:req.params.user 不是函数” - Unable to route using express and mongoose, keep getting "Type Error : req.params.user is not a function" 如何在node.js中杀死child_process —要求关闭 - How to kill child_process in node.js — req.on close 为什么控制不在 req.on(“data”, data => { }); 我正在尝试处理 POST 方法数据 - Why do control is not going inside req.on(“data”, data => { }); I'm trying to handle POST method data 在 app.js 中导入 js 文件时出现未知文件扩展名错误,使用类型:package.json 中的模块 - I am getting unknown file extension error while importing a js file in app.js, using type: module in package.json 如何在节点js中使用带multer的FormData上传文件,在req.file中未定义,在req.body中获取{} - How to upload file using FormData with multer in node js, getting undefined in req.file and { } in req.body
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM