\href{https://npmjs.org/package/on-finished}{\texttt{ }} \href{https://npmjs.org/package/on-finished}{\texttt{ }} \href{http://nodejs.org/download/}{\texttt{ }} \href{https://travis-ci.org/jshttp/on-finished}{\texttt{ }} \href{https://coveralls.io/r/jshttp/on-finished?branch=master}{\texttt{ }}

Execute a callback when a HTTP request closes, finishes, or errors.\hypertarget{md_sfcode_node_modules_on_finished_README_autotoc_md19056}{}\doxysection{Install}\label{md_sfcode_node_modules_on_finished_README_autotoc_md19056}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install on-\/finished }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_on_finished_README_autotoc_md19057}{}\doxysection{API}\label{md_sfcode_node_modules_on_finished_README_autotoc_md19057}

\begin{DoxyCode}{0}
\DoxyCodeLine{var onFinished = require('on-\/finished') }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_on_finished_README_autotoc_md19058}{}\doxysubsection{on\+Finished(res, listener)}\label{md_sfcode_node_modules_on_finished_README_autotoc_md19058}
Attach a listener to listen for the response to finish. The listener will be invoked only once when the response finished. If the response finished to an error, the first argument will contain the error. If the response has already finished, the listener will be invoked.

Listening to the end of a response would be used to close things associated with the response, like open files.

Listener is invoked as {\ttfamily listener(err, res)}.


\begin{DoxyCode}{0}
\DoxyCodeLine{onFinished(res, function (err, res) \{}
\DoxyCodeLine{  // clean up open fds, etc.}
\DoxyCodeLine{  // err contains the error is request error'd}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_on_finished_README_autotoc_md19059}{}\doxysubsection{on\+Finished(req, listener)}\label{md_sfcode_node_modules_on_finished_README_autotoc_md19059}
Attach a listener to listen for the request to finish. The listener will be invoked only once when the request finished. If the request finished to an error, the first argument will contain the error. If the request has already finished, the listener will be invoked.

Listening to the end of a request would be used to know when to continue after reading the data.

Listener is invoked as {\ttfamily listener(err, req)}.


\begin{DoxyCode}{0}
\DoxyCodeLine{var data = ''}
\DoxyCodeLine{}
\DoxyCodeLine{req.setEncoding('utf8')}
\DoxyCodeLine{res.on('data', function (str) \{}
\DoxyCodeLine{  data += str}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{onFinished(req, function (err, req) \{}
\DoxyCodeLine{  // data is read unless there is err}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_on_finished_README_autotoc_md19060}{}\doxysubsection{on\+Finished.\+is\+Finished(res)}\label{md_sfcode_node_modules_on_finished_README_autotoc_md19060}
Determine if {\ttfamily res} is already finished. This would be useful to check and not even start certain operations if the response has already finished.\hypertarget{md_sfcode_node_modules_on_finished_README_autotoc_md19061}{}\doxysubsection{on\+Finished.\+is\+Finished(req)}\label{md_sfcode_node_modules_on_finished_README_autotoc_md19061}
Determine if {\ttfamily req} is already finished. This would be useful to check and not even start certain operations if the request has already finished.\hypertarget{md_sfcode_node_modules_on_finished_README_autotoc_md19062}{}\doxysection{Special Node.\+js requests}\label{md_sfcode_node_modules_on_finished_README_autotoc_md19062}
\hypertarget{md_sfcode_node_modules_on_finished_README_autotoc_md19063}{}\doxysubsection{HTTP CONNECT method}\label{md_sfcode_node_modules_on_finished_README_autotoc_md19063}
The meaning of the {\ttfamily CONNECT} method from RFC 7231, section 4.\+3.\+6\+:

\begin{quote}
The CONNECT method requests that the recipient establish a tunnel to the destination origin server identified by the request-\/target and, if successful, thereafter restrict its behavior to blind forwarding of packets, in both directions, until the tunnel is closed. Tunnels are commonly used to create an end-\/to-\/end virtual connection, through one or more proxies, which can then be secured using TLS (Transport Layer Security, \mbox{[}RFC5246\mbox{]}). \end{quote}
In Node.\+js, these request objects come from the `\textquotesingle{}connect'\`{} event on the HTTP server.

When this module is used on a HTTP {\ttfamily CONNECT} request, the request is considered \char`\"{}finished\char`\"{} immediately, {\bfseries{due to limitations in the Node.\+js interface}}. This means if the {\ttfamily CONNECT} request contains a request entity, the request will be considered \char`\"{}finished\char`\"{} even before it has been read.

There is no such thing as a response object to a {\ttfamily CONNECT} request in Node.\+js, so there is no support for for one.\hypertarget{md_sfcode_node_modules_on_finished_README_autotoc_md19064}{}\doxysubsection{HTTP Upgrade request}\label{md_sfcode_node_modules_on_finished_README_autotoc_md19064}
The meaning of the {\ttfamily Upgrade} header from RFC 7230, section 6.\+1\+:

\begin{quote}
The \char`\"{}\+Upgrade\char`\"{} header field is intended to provide a simple mechanism for transitioning from HTTP/1.\+1 to some other protocol on the same connection. \end{quote}
In Node.\+js, these request objects come from the `\textquotesingle{}upgrade'\`{} event on the HTTP server.

When this module is used on a HTTP request with an {\ttfamily Upgrade} header, the request is considered \char`\"{}finished\char`\"{} immediately, {\bfseries{due to limitations in the Node.\+js interface}}. This means if the {\ttfamily Upgrade} request contains a request entity, the request will be considered \char`\"{}finished\char`\"{} even before it has been read.

There is no such thing as a response object to a {\ttfamily Upgrade} request in Node.\+js, so there is no support for for one.\hypertarget{md_sfcode_node_modules_on_finished_README_autotoc_md19065}{}\doxysection{Example}\label{md_sfcode_node_modules_on_finished_README_autotoc_md19065}
The following code ensures that file descriptors are always closed once the response finishes.


\begin{DoxyCode}{0}
\DoxyCodeLine{var destroy = require('destroy')}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{var onFinished = require('on-\/finished')}
\DoxyCodeLine{}
\DoxyCodeLine{http.createServer(function onRequest(req, res) \{}
\DoxyCodeLine{  var stream = fs.createReadStream('package.json')}
\DoxyCodeLine{  stream.pipe(res)}
\DoxyCodeLine{  onFinished(res, function (err) \{}
\DoxyCodeLine{    destroy(stream)}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_on_finished_README_autotoc_md19066}{}\doxysection{License}\label{md_sfcode_node_modules_on_finished_README_autotoc_md19066}
\mbox{[}MIT\mbox{]}(LICENSE) 