Skip to content

Commit aeef951

Browse files
committed
doc: Correct caveats for http Readables
1 parent 58f93ff commit aeef951

1 file changed

Lines changed: 11 additions & 59 deletions

File tree

doc/api/http.markdown

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -557,28 +557,14 @@ headers have been received. The `'response'` event is executed with one
557557
argument which is an instance of `http.IncomingMessage`.
558558

559559
During the `'response'` event, one can add listeners to the
560-
response object; particularly to listen for the `'data'` event. Note that
561-
the `'response'` event is called before any part of the response body is received,
562-
so there is no need to worry about racing to catch the first part of the
563-
body. As long as a listener for `'data'` is added during the `'response'`
564-
event, the entire body will be caught.
560+
response object; particularly to listen for the `'data'` event.
565561

566-
567-
// Good
568-
request.on('response', function (response) {
569-
response.on('data', function (chunk) {
570-
console.log('BODY: ' + chunk);
571-
});
572-
});
573-
574-
// Bad - misses all or part of the body
575-
request.on('response', function (response) {
576-
setTimeout(function () {
577-
response.on('data', function (chunk) {
578-
console.log('BODY: ' + chunk);
579-
});
580-
}, 10);
581-
});
562+
If no `'response'` handler is added, then the response will be
563+
entirely discarded. However, if you add a `'response'` event handler,
564+
then you **must** consume the data from the response object, either by
565+
calling `response.read()` whenever there is a `'readable'` event, or
566+
by adding a `'data'` handler, or by calling the `.resume()` method.
567+
Until the data is consumed, the `'end'` event will not fire.
582568

583569
Note: Node does not check whether Content-Length and the length of the body
584570
which has been transmitted are equal or not.
@@ -774,26 +760,8 @@ An `IncomingMessage` object is created by `http.Server` or `http.ClientRequest`
774760
and passed as the first argument to the `'request'` and `'response'` event
775761
respectively. It may be used to access response status, headers and data.
776762

777-
It implements the [Readable Stream][] interface. `http.IncomingMessage` is an
778-
[EventEmitter][] with the following events:
779-
780-
### Event: 'data'
781-
782-
`function (chunk) { }`
783-
784-
Emitted when a piece of the message body is received. The chunk is a string if
785-
an encoding has been set with `message.setEncoding()`, otherwise it's
786-
a [Buffer][].
787-
788-
Note that the __data will be lost__ if there is no listener when a
789-
`IncomingMessage` emits a `'data'` event.
790-
791-
### Event: 'end'
792-
793-
`function () { }`
794-
795-
Emitted exactly once for each response. After that, no more `'data'` events
796-
will be emitted on the response.
763+
It implements the [Readable Stream][] interface, as well as the
764+
following additional events, methods, and properties.
797765

798766
### Event: 'close'
799767

@@ -802,9 +770,8 @@ will be emitted on the response.
802770
Indicates that the underlaying connection was terminated before
803771
`response.end()` was called or able to flush.
804772

805-
Just like `'end'`, this event occurs only once per response, and no more
806-
`'data'` events will fire afterwards. See [http.ServerResponse][]'s `'close'`
807-
event for more information.
773+
Just like `'end'`, this event occurs only once per response. See
774+
[http.ServerResponse][]'s `'close'` event for more information.
808775

809776
### message.httpVersion
810777

@@ -840,21 +807,6 @@ The request/response trailers object. Only populated after the 'end' event.
840807

841808
Calls `message.connection.setTimeout(msecs, callback)`.
842809

843-
### message.setEncoding([encoding])
844-
845-
Set the encoding for data emitted by the `'data'` event. See [stream.setEncoding()][] for more
846-
information.
847-
848-
Should be set before any `'data'` events have been emitted.
849-
850-
### message.pause()
851-
852-
Pauses request/response from emitting events. Useful to throttle back a download.
853-
854-
### message.resume()
855-
856-
Resumes a paused request/response.
857-
858810
### message.method
859811

860812
**Only valid for request obtained from `http.Server`.**

0 commit comments

Comments
 (0)