简体   繁体   English

Node.js:http完成事件,响应关闭事件和响应结束事件之间的区别

[英]Node.js: Difference between http finish event, response close event, and response end event

I'm new to Node.js and after looking in the documentation and experimenting with the http.on('finish'), res.on('close'), and res.on('end'), I do not understand how each are different. 我是Node.js的新手,在浏览了文档并尝试了http.on('finish'),res.on('close')和res.on('end')之后,我不明白两者有何不同。

 http.get(url, res => { res.setEncoding('utf8'); // returns string object to data event res.on('data', string => { const responseObj = responseDataList.find(responseObj => { return responseObj.url === url; }); responseObj.data += string; }); res.on('close', () => { console.log('[Interruption] connection closed before response was ended.'); // Never fires }) res.on('error', console.error); // TODO: find out difference between response.end and response.close and http.finish events res.on('end', (data, encoding) => { // Seems to fire at the same time that http.on('finish') fires current++; if (counter === current) { responseDataList.forEach(responseObj => { console.log(responseObj.data); }) } }); }) .on('error', console.error) .on('finish', () => { // Seems to fire at the same time that res.on('end') fires console.log('response sent') }); // emitted when the response is sent to the OS (not when it is received by the client) 

When do http.on('finish'), res.on('end'), res.on('close') fire and how are they different? http.on('finish'),res.on('end'),res.on('close')何时启动,它们有何不同?

As soon as http.get calls your callback with the res object it returns an http.ClientRequest . 一旦http.get使用res对象调用回调,它将返回一个http.ClientRequest The http.ClientRequest inherits from Stream . http.ClientRequest继承自Stream

So, according to the docs : 因此,根据文档

The finish event is emitted after the stream.end() method has been called, and all data has been flushed to the underlying system. stream.end()方法并将所有数据刷新到基础系统之后,将发出finish事件。

In the case of http.get , stream.end() is called immediately after making the request (See here ). 对于http.get ,在发出请求后立即调用stream.end() (请参阅此处 )。 NB calling stream.end() is different from listening to the res.on('end') event. 注意,调用stream.end()与监听res.on('end')事件有所不同。

So for http.get , the finish event will fire immediately after making the request and then the res object events will start firing. 因此,对于http.getfinish事件将在发出请求后立即触发,然后res对象事件将开始触发。

Your res object is an HTTP.IncomingMessage which implements the Readable Stream interface 您的res对象是一个HTTP.IncomingMessage ,它实现了Readable Stream接口

According the the Readable Stream docs: 根据Readable Stream文档:

The 'end' event is emitted when there is no more data to be consumed from the stream. 当没有更多数据要从流中使用时,将发出“结束”事件。

So end fires first then close 所以, end的火灾第一,然后close

Also for Readable Streams 也适用于Readable Streams

The 'close' event is emitted when the stream and any of its underlying resources (a file descriptor, for example) have been closed. 当流及其任何基础资源(例如文件描述符)已关闭时,将发出“ close”事件。 The event indicates that no more events will be emitted, and no further computation will occur. 该事件表明将不再发出事件,并且不会进行进一步的计算。

So the events fire in this order: finish , end , close 因此事件按以下顺序触发: finishendclose

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

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