\href{https://npmjs.org/package/finalhandler}{\texttt{ }} \href{https://npmjs.org/package/finalhandler}{\texttt{ }} \href{https://nodejs.org/en/download}{\texttt{ }} \href{https://travis-ci.org/pillarjs/finalhandler}{\texttt{ }} \href{https://coveralls.io/r/pillarjs/finalhandler?branch=master}{\texttt{ }}

Node.\+js function to invoke as the final step to respond to HTTP request.\hypertarget{md_sfcode_node_modules_finalhandler_README_autotoc_md10647}{}\doxysection{Installation}\label{md_sfcode_node_modules_finalhandler_README_autotoc_md10647}
This is a \href{https://nodejs.org/en/}{\texttt{ Node.\+js}} module available through the \href{https://www.npmjs.com/}{\texttt{ npm registry}}. Installation is done using the \href{https://docs.npmjs.com/getting-started/installing-npm-packages-locally}{\texttt{ {\ttfamily npm install} command}}\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm install finalhandler }

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

\begin{DoxyCode}{0}
\DoxyCodeLine{var finalhandler = require('finalhandler') }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_finalhandler_README_autotoc_md10649}{}\doxysubsection{finalhandler(req, res, \mbox{[}options\mbox{]})}\label{md_sfcode_node_modules_finalhandler_README_autotoc_md10649}
Returns function to be invoked as the final step for the given {\ttfamily req} and {\ttfamily res}. This function is to be invoked as {\ttfamily fn(err)}. If {\ttfamily err} is falsy, the handler will write out a 404 response to the {\ttfamily res}. If it is truthy, an error response will be written out to the {\ttfamily res}.

When an error is written, the following information is added to the response\+:


\begin{DoxyItemize}
\item The {\ttfamily res.\+status\+Code} is set from {\ttfamily err.\+status} (or {\ttfamily err.\+status\+Code}). If this value is outside the 4xx or 5xx range, it will be set to 500.
\item The {\ttfamily res.\+status\+Message} is set according to the status code.
\item The body will be the HTML of the status code message if {\ttfamily env} is `\textquotesingle{}production'{\ttfamily , otherwise will be}err.\+stack$<$tt$>$.
\item Any headers specified in anerr.\+headers\`{} object.
\end{DoxyItemize}

The final handler will also unpipe anything from {\ttfamily req} when it is invoked.\hypertarget{md_sfcode_node_modules_finalhandler_README_autotoc_md10650}{}\doxysubsubsection{options.\+env}\label{md_sfcode_node_modules_finalhandler_README_autotoc_md10650}
By default, the environment is determined by {\ttfamily NODE\+\_\+\+ENV} variable, but it can be overridden by this option.\hypertarget{md_sfcode_node_modules_finalhandler_README_autotoc_md10651}{}\doxysubsubsection{options.\+onerror}\label{md_sfcode_node_modules_finalhandler_README_autotoc_md10651}
Provide a function to be called with the {\ttfamily err} when it exists. Can be used for writing errors to a central location without excessive function generation. Called as {\ttfamily onerror(err, req, res)}.\hypertarget{md_sfcode_node_modules_finalhandler_README_autotoc_md10652}{}\doxysection{Examples}\label{md_sfcode_node_modules_finalhandler_README_autotoc_md10652}
\hypertarget{md_sfcode_node_modules_finalhandler_README_autotoc_md10653}{}\doxysubsection{always 404}\label{md_sfcode_node_modules_finalhandler_README_autotoc_md10653}

\begin{DoxyCode}{0}
\DoxyCodeLine{var finalhandler = require('finalhandler')}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{}
\DoxyCodeLine{var server = http.createServer(function (req, res) \{}
\DoxyCodeLine{  var done = finalhandler(req, res)}
\DoxyCodeLine{  done()}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_finalhandler_README_autotoc_md10654}{}\doxysubsection{perform simple action}\label{md_sfcode_node_modules_finalhandler_README_autotoc_md10654}

\begin{DoxyCode}{0}
\DoxyCodeLine{var finalhandler = require('finalhandler')}
\DoxyCodeLine{var fs = require('fs')}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{}
\DoxyCodeLine{var server = http.createServer(function (req, res) \{}
\DoxyCodeLine{  var done = finalhandler(req, res)}
\DoxyCodeLine{}
\DoxyCodeLine{  fs.readFile('index.html', function (err, buf) \{}
\DoxyCodeLine{    if (err) return done(err)}
\DoxyCodeLine{    res.setHeader('Content-\/Type', 'text/html')}
\DoxyCodeLine{    res.end(buf)}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_finalhandler_README_autotoc_md10655}{}\doxysubsection{use with middleware-\/style functions}\label{md_sfcode_node_modules_finalhandler_README_autotoc_md10655}

\begin{DoxyCode}{0}
\DoxyCodeLine{var finalhandler = require('finalhandler')}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{var serveStatic = require('serve-\/static')}
\DoxyCodeLine{}
\DoxyCodeLine{var serve = serveStatic('public')}
\DoxyCodeLine{}
\DoxyCodeLine{var server = http.createServer(function (req, res) \{}
\DoxyCodeLine{  var done = finalhandler(req, res)}
\DoxyCodeLine{  serve(req, res, done)}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_finalhandler_README_autotoc_md10656}{}\doxysubsection{keep log of all errors}\label{md_sfcode_node_modules_finalhandler_README_autotoc_md10656}

\begin{DoxyCode}{0}
\DoxyCodeLine{var finalhandler = require('finalhandler')}
\DoxyCodeLine{var fs = require('fs')}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{}
\DoxyCodeLine{var server = http.createServer(function (req, res) \{}
\DoxyCodeLine{  var done = finalhandler(req, res, \{ onerror: logerror \})}
\DoxyCodeLine{}
\DoxyCodeLine{  fs.readFile('index.html', function (err, buf) \{}
\DoxyCodeLine{    if (err) return done(err)}
\DoxyCodeLine{    res.setHeader('Content-\/Type', 'text/html')}
\DoxyCodeLine{    res.end(buf)}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\})}
\DoxyCodeLine{}
\DoxyCodeLine{server.listen(3000)}
\DoxyCodeLine{}
\DoxyCodeLine{function logerror (err) \{}
\DoxyCodeLine{  console.error(err.stack || err.toString())}
\DoxyCodeLine{\} }

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