3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Node.js Version][node-version-image]][node-version-url]
6 [![Build Status][travis-image]][travis-url]
7 [![Test Coverage][coveralls-image]][coveralls-url]
9 Execute a callback when a HTTP request closes, finishes, or errors.
14 $ npm install on-finished
20 var onFinished = require('on-finished')
23 ### onFinished(res, listener)
25 Attach a listener to listen for the response to finish. The listener will
26 be invoked only once when the response finished. If the response finished
27 to an error, the first argument will contain the error. If the response
28 has already finished, the listener will be invoked.
30 Listening to the end of a response would be used to close things associated
31 with the response, like open files.
33 Listener is invoked as `listener(err, res)`.
36 onFinished(res, function (err, res) {
37 // clean up open fds, etc.
38 // err contains the error is request error'd
42 ### onFinished(req, listener)
44 Attach a listener to listen for the request to finish. The listener will
45 be invoked only once when the request finished. If the request finished
46 to an error, the first argument will contain the error. If the request
47 has already finished, the listener will be invoked.
49 Listening to the end of a request would be used to know when to continue
50 after reading the data.
52 Listener is invoked as `listener(err, req)`.
57 req.setEncoding('utf8')
58 res.on('data', function (str) {
62 onFinished(req, function (err, req) {
63 // data is read unless there is err
67 ### onFinished.isFinished(res)
69 Determine if `res` is already finished. This would be useful to check and
70 not even start certain operations if the response has already finished.
72 ### onFinished.isFinished(req)
74 Determine if `req` is already finished. This would be useful to check and
75 not even start certain operations if the request has already finished.
77 ## Special Node.js requests
79 ### HTTP CONNECT method
81 The meaning of the `CONNECT` method from RFC 7231, section 4.3.6:
83 > The CONNECT method requests that the recipient establish a tunnel to
84 > the destination origin server identified by the request-target and,
85 > if successful, thereafter restrict its behavior to blind forwarding
86 > of packets, in both directions, until the tunnel is closed. Tunnels
87 > are commonly used to create an end-to-end virtual connection, through
88 > one or more proxies, which can then be secured using TLS (Transport
89 > Layer Security, [RFC5246]).
91 In Node.js, these request objects come from the `'connect'` event on
94 When this module is used on a HTTP `CONNECT` request, the request is
95 considered "finished" immediately, **due to limitations in the Node.js
96 interface**. This means if the `CONNECT` request contains a request entity,
97 the request will be considered "finished" even before it has been read.
99 There is no such thing as a response object to a `CONNECT` request in
100 Node.js, so there is no support for for one.
102 ### HTTP Upgrade request
104 The meaning of the `Upgrade` header from RFC 7230, section 6.1:
106 > The "Upgrade" header field is intended to provide a simple mechanism
107 > for transitioning from HTTP/1.1 to some other protocol on the same
110 In Node.js, these request objects come from the `'upgrade'` event on
113 When this module is used on a HTTP request with an `Upgrade` header, the
114 request is considered "finished" immediately, **due to limitations in the
115 Node.js interface**. This means if the `Upgrade` request contains a request
116 entity, the request will be considered "finished" even before it has been
119 There is no such thing as a response object to a `Upgrade` request in
120 Node.js, so there is no support for for one.
124 The following code ensures that file descriptors are always closed
125 once the response finishes.
128 var destroy = require('destroy')
129 var http = require('http')
130 var onFinished = require('on-finished')
132 http.createServer(function onRequest(req, res) {
133 var stream = fs.createReadStream('package.json')
135 onFinished(res, function (err) {
145 [npm-image]: https://img.shields.io/npm/v/on-finished.svg
146 [npm-url]: https://npmjs.org/package/on-finished
147 [node-version-image]: https://img.shields.io/node/v/on-finished.svg
148 [node-version-url]: http://nodejs.org/download/
149 [travis-image]: https://img.shields.io/travis/jshttp/on-finished/master.svg
150 [travis-url]: https://travis-ci.org/jshttp/on-finished
151 [coveralls-image]: https://img.shields.io/coveralls/jshttp/on-finished/master.svg
152 [coveralls-url]: https://coveralls.io/r/jshttp/on-finished?branch=master
153 [downloads-image]: https://img.shields.io/npm/dm/on-finished.svg
154 [downloads-url]: https://npmjs.org/package/on-finished