简体   繁体   English

NodeJS请求对象何时发出事件?

[英]When does the NodeJS request object emit events?

It seems I can attach multiple handlers to a request and they will all receive events in the right order/time. 看来我可以将多个处理程序附加到一个request并且它们都将按正确的顺序/时间接收事件。 When exactly does a request , which has already been received and emitted by the server - when does it emit the data/end events? 服务器何时已正确接收并发出了一个request ,它何时发出数据/结束事件?

function handler1(req, res){
   req.on('data', function(data){{
       console.log("Handler 1 data");
   });
   req.on('end', function(){{
       console.log("Handler 1 end");
   });
}

function handler2(req, res){
   req.on('data', function(data){{
       console.log("Handler 2 data");
   });
   req.on('end', function(){{
       console.log("Handler 2 end");
   });
}

http.on('request', function(req, res){
   handler1(req, res);
   handler2(req, res);
});

// Handler1 data
// Handler2 data
// Handler1 end
// Handler2 end

The request object is an EventEmitter object. 请求对象是一个EventEmitter对象。 As such, it emits events and anyone can add listeners for those events. 这样,它会发出事件,任何人都可以为这些事件添加侦听器。

In the case of the request object, it emits events that correspond with networking. 对于请求对象,它发出与联网相对应的事件。 The data event is emitted when some data has arrived (from the request body). 当某些数据到达时(从请求主体),将发出data事件。 If you have more than one listener for the data event, then each listener will see each data event with the exact same data. 如果您有一个以上的data事件侦听器,则每个侦听器将看到每个data事件具有完全相同的数据。

The end event is emitted where there is no more data to from the request body (all data has arrived). 在没有更多数据要从请求主体发送(所有数据已到达)的地方发出end事件。

Until you attach some means of reading the data from the incoming request (such as a data event listener), the data is queued and will be held in memory. 除非您使用某种方法从传入的请求中读取数据(例如, data事件侦听器),否则数据将排队并保存在内存中。 This gives you a chance to attach the listener even after the data has already started arriving without any worry about missing any of the data. 即使在数据已经开始到达后,这也使您有机会连接侦听器,而不必担心丢失任何数据。 In node.js, many streams work this way. 在node.js中,许多流都以这种方式工作。 Data doesn't start flowing through the listeners until you attach at least one listener for the data. 直到您为数据附加至少一个侦听器,数据才开始流经侦听器。

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

相关问题 socket.emit()在创建套接字对象后立即使用时不起作用,但是在单击事件时调用它时起作用? - socket.emit() does not work when used immediately after creating the socket object but works when i call it on click events? nodejs不向客户端发送数据 - nodejs does not emit data to client NodeJS&Socket.IO:发出请求事件并获取响应,何时/何处应该绑定侦听器? - NodeJS & Socket.IO: Emit a request event and get the response, when/where should I bind the listener? 返回只能接收事件但不能发出事件的对象 - Returning an object that can only receive events but not emit _http_server.js-尚无request('events').emit()&& .on()如何? - _http_server.js - no request('events') yet .emit() && .on() how? 数组 object 上的扩展运算符不发出属性 - spread operator on array object does not emit properties Angular.js - 表单验证会发出任何事件吗? - Angular.js - does form validation emit any events? 使用http.request时,Nodejs的性能如何扩展? - How does Nodejs performance scale when using http.request? iOS中的ReactNative FusionChart事件不会发出/触发。 只有第一个会发出 - ReactNative FusionChart events in iOS does not emit / fire. Only the first will emit 热观测何时发出第一值? - When does hot observable emit first value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM