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

Higher level content negotiation based on \href{https://www.npmjs.com/package/negotiator}{\texttt{ negotiator}}. Extracted from \href{https://www.npmjs.com/package/koa}{\texttt{ koa}} for general use.

In addition to negotiator, it allows\+:


\begin{DoxyItemize}
\item Allows types as an array or arguments list, ie `(\mbox{[}\textquotesingle{}text/html', \textquotesingle{}application/json\textquotesingle{}\mbox{]}){\ttfamily  as well as}(\textquotesingle{}text/html\textquotesingle{}, \textquotesingle{}application/json\textquotesingle{}){\ttfamily .}
\item {\ttfamily Allows type shorthands such as}json{\ttfamily .}
\item {\ttfamily Returns}false{\ttfamily when no types match}
\item {\ttfamily Treats non-\/existent headers as}$\ast$\`{}
\end{DoxyItemize}\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2376}{}\doxysection{Installation}\label{md_sfcode_node_modules_accepts_README_autotoc_md2376}
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 accepts }

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

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

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2378}{}\doxysubsection{accepts(req)}\label{md_sfcode_node_modules_accepts_README_autotoc_md2378}
Create a new {\ttfamily Accepts} object for the given {\ttfamily req}.\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2379}{}\doxysubsubsection{.\+charset(charsets)}\label{md_sfcode_node_modules_accepts_README_autotoc_md2379}
Return the first accepted charset. If nothing in {\ttfamily charsets} is accepted, then {\ttfamily false} is returned.\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2380}{}\doxysubsubsection{.\+charsets()}\label{md_sfcode_node_modules_accepts_README_autotoc_md2380}
Return the charsets that the request accepts, in the order of the client\textquotesingle{}s preference (most preferred first).\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2381}{}\doxysubsubsection{.\+encoding(encodings)}\label{md_sfcode_node_modules_accepts_README_autotoc_md2381}
Return the first accepted encoding. If nothing in {\ttfamily encodings} is accepted, then {\ttfamily false} is returned.\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2382}{}\doxysubsubsection{.\+encodings()}\label{md_sfcode_node_modules_accepts_README_autotoc_md2382}
Return the encodings that the request accepts, in the order of the client\textquotesingle{}s preference (most preferred first).\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2383}{}\doxysubsubsection{.\+language(languages)}\label{md_sfcode_node_modules_accepts_README_autotoc_md2383}
Return the first accepted language. If nothing in {\ttfamily languages} is accepted, then {\ttfamily false} is returned.\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2384}{}\doxysubsubsection{.\+languages()}\label{md_sfcode_node_modules_accepts_README_autotoc_md2384}
Return the languages that the request accepts, in the order of the client\textquotesingle{}s preference (most preferred first).\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2385}{}\doxysubsubsection{.\+type(types)}\label{md_sfcode_node_modules_accepts_README_autotoc_md2385}
Return the first accepted type (and it is returned as the same text as what appears in the {\ttfamily types} array). If nothing in {\ttfamily types} is accepted, then {\ttfamily false} is returned.

The {\ttfamily types} array can contain full MIME types or file extensions. Any value that is not a full MIME types is passed to `require(\textquotesingle{}mime-\/types').lookup\`{}.\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2386}{}\doxysubsubsection{.\+types()}\label{md_sfcode_node_modules_accepts_README_autotoc_md2386}
Return the types that the request accepts, in the order of the client\textquotesingle{}s preference (most preferred first).\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2387}{}\doxysection{Examples}\label{md_sfcode_node_modules_accepts_README_autotoc_md2387}
\hypertarget{md_sfcode_node_modules_accepts_README_autotoc_md2388}{}\doxysubsection{Simple type negotiation}\label{md_sfcode_node_modules_accepts_README_autotoc_md2388}
This simple example shows how to use {\ttfamily accepts} to return a different typed respond body based on what the client wants to accept. The server lists it\textquotesingle{}s preferences in order and will get back the best match between the client and server.


\begin{DoxyCode}{0}
\DoxyCodeLine{var accepts = require('accepts')}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{}
\DoxyCodeLine{function app (req, res) \{}
\DoxyCodeLine{  var accept = accepts(req)}
\DoxyCodeLine{}
\DoxyCodeLine{  // the order of this list is significant; should be server preferred order}
\DoxyCodeLine{  switch (accept.type(['json', 'html'])) \{}
\DoxyCodeLine{    case 'json':}
\DoxyCodeLine{      res.setHeader('Content-\/Type', 'application/json')}
\DoxyCodeLine{      res.write('\{"{}hello"{}:"{}world!"{}\}')}
\DoxyCodeLine{      break}
\DoxyCodeLine{    case 'html':}
\DoxyCodeLine{      res.setHeader('Content-\/Type', 'text/html')}
\DoxyCodeLine{      res.write('<b>hello, world!</b>')}
\DoxyCodeLine{      break}
\DoxyCodeLine{    default:}
\DoxyCodeLine{      // the fallback is text/plain, so no need to specify it above}
\DoxyCodeLine{      res.setHeader('Content-\/Type', 'text/plain')}
\DoxyCodeLine{      res.write('hello, world!')}
\DoxyCodeLine{      break}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  res.end()}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{http.createServer(app).listen(3000) }

\end{DoxyCode}


You can test this out with the c\+URL program\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{curl -\/I -\/H'Accept: text/html' http://localhost:3000/ }

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