Skip to content

Commit d768ed8

Browse files
committedMar 12, 2016
docs: note stream error handling and destruction. ref: #612
1 parent 86a6f2b commit d768ed8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
 

‎docs/api/response.md

+17
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,23 @@ If `response.status` has not been set, Koa will automatically set the status to
136136

137137
The Content-Type is defaulted to application/octet-stream.
138138

139+
Whenever a stream is set as the response body, `.onerror` is automatically added as a listener to the `error` event to catch any errors.
140+
In addition, whenever the request is closed (even prematurely), the stream is destroyed.
141+
If you do not want these two features, do not set the stream as the body directly.
142+
For example, you may not want this when setting the body as an HTTP stream in a proxy as it would destroy the underlying connection.
143+
144+
See: https://github.com/koajs/koa/pull/612 for more information.
145+
146+
Here's an example of stream error handling without automatically destroying the stream:
147+
148+
```js
149+
const PassThrough = require('stream').PassThrough
150+
151+
app.use(function * (next) {
152+
this.body = someHTTPStream.on('error', this.onerror).pipe(PassThrough())
153+
})
154+
```
155+
139156
#### Object
140157

141158
The Content-Type is defaulted to application/json.

0 commit comments

Comments
 (0)
Please sign in to comment.