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

Execute a listener when a response is about to write headers.\hypertarget{md_sfcode_node_modules_on_headers_README_autotoc_md19071}{}\doxysection{Installation}\label{md_sfcode_node_modules_on_headers_README_autotoc_md19071}
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 on-\/headers }

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

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

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_on_headers_README_autotoc_md19073}{}\doxysubsection{on\+Headers(res, listener)}\label{md_sfcode_node_modules_on_headers_README_autotoc_md19073}
This will add the listener {\ttfamily listener} to fire when headers are emitted for {\ttfamily res}. The listener is passed the {\ttfamily response} object as it\textquotesingle{}s context ({\ttfamily this}). Headers are considered to be emitted only once, right before they are sent to the client.

When this is called multiple times on the same {\ttfamily res}, the {\ttfamily listener}s are fired in the reverse order they were added.\hypertarget{md_sfcode_node_modules_on_headers_README_autotoc_md19074}{}\doxysection{Examples}\label{md_sfcode_node_modules_on_headers_README_autotoc_md19074}

\begin{DoxyCode}{0}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{var onHeaders = require('on-\/headers')}
\DoxyCodeLine{}
\DoxyCodeLine{http}
\DoxyCodeLine{  .createServer(onRequest)}
\DoxyCodeLine{  .listen(3000)}
\DoxyCodeLine{}
\DoxyCodeLine{function addPoweredBy () \{}
\DoxyCodeLine{  // set if not set by end of request}
\DoxyCodeLine{  if (!this.getHeader('X-\/Powered-\/By')) \{}
\DoxyCodeLine{    this.setHeader('X-\/Powered-\/By', 'Node.js')}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{function onRequest (req, res) \{}
\DoxyCodeLine{  onHeaders(res, addPoweredBy)}
\DoxyCodeLine{}
\DoxyCodeLine{  res.setHeader('Content-\/Type', 'text/plain')}
\DoxyCodeLine{  res.end('hello!')}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_on_headers_README_autotoc_md19075}{}\doxysection{Testing}\label{md_sfcode_node_modules_on_headers_README_autotoc_md19075}

\begin{DoxyCode}{0}
\DoxyCodeLine{\$ npm test }

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