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

Infer the content-\/type of a request.\hypertarget{md_sfcode_node_modules_type_is_README_autotoc_md31230}{}\doxysubsection{Install}\label{md_sfcode_node_modules_type_is_README_autotoc_md31230}
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 type-\/is }

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

\begin{DoxyCode}{0}
\DoxyCodeLine{var http = require('http')}
\DoxyCodeLine{var typeis = require('type-\/is')}
\DoxyCodeLine{}
\DoxyCodeLine{http.createServer(function (req, res) \{}
\DoxyCodeLine{  var istext = typeis(req, ['text/*'])}
\DoxyCodeLine{  res.end('you ' + (istext ? 'sent' : 'did not send') + ' me text')}
\DoxyCodeLine{\}) }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_type_is_README_autotoc_md31232}{}\doxysubsection{typeis(request, types)}\label{md_sfcode_node_modules_type_is_README_autotoc_md31232}
Checks if the {\ttfamily request} is one of the {\ttfamily types}. If the request has no body, even if there is a {\ttfamily Content-\/\+Type} header, then {\ttfamily null} is returned. If the {\ttfamily Content-\/\+Type} header is invalid or does not matches any of the {\ttfamily types}, then {\ttfamily false} is returned. Otherwise, a string of the type that matched is returned.

The {\ttfamily request} argument is expected to be a Node.\+js HTTP request. The {\ttfamily types} argument is an array of type strings.

Each type in the {\ttfamily types} array can be one of the following\+:


\begin{DoxyItemize}
\item A file extension name such as {\ttfamily json}. This name will be returned if matched.
\item A mime type such as {\ttfamily application/json}.
\item A mime type with a wildcard such as {\ttfamily $\ast$/$\ast$} or {\ttfamily $\ast$/json} or {\ttfamily application/$\ast$}. The full mime type will be returned if matched.
\item A suffix such as {\ttfamily +json}. This can be combined with a wildcard such as {\ttfamily $\ast$/vnd+json} or {\ttfamily application/$\ast$+json}. The full mime type will be returned if matched.
\end{DoxyItemize}

Some examples to illustrate the inputs and returned value\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{// req.headers.content-\/type = 'application/json'}
\DoxyCodeLine{}
\DoxyCodeLine{typeis(req, ['json']) // => 'json'}
\DoxyCodeLine{typeis(req, ['html', 'json']) // => 'json'}
\DoxyCodeLine{typeis(req, ['application/*']) // => 'application/json'}
\DoxyCodeLine{typeis(req, ['application/json']) // => 'application/json'}
\DoxyCodeLine{}
\DoxyCodeLine{typeis(req, ['html']) // => false }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_type_is_README_autotoc_md31233}{}\doxysubsection{typeis.\+has\+Body(request)}\label{md_sfcode_node_modules_type_is_README_autotoc_md31233}
Returns a Boolean if the given {\ttfamily request} has a body, regardless of the {\ttfamily Content-\/\+Type} header.

Having a body has no relation to how large the body is (it may be 0 bytes). This is similar to how file existence works. If a body does exist, then this indicates that there is data to read from the Node.\+js request stream.


\begin{DoxyCode}{0}
\DoxyCodeLine{if (typeis.hasBody(req)) \{}
\DoxyCodeLine{  // read the body, since there is one}
\DoxyCodeLine{}
\DoxyCodeLine{  req.on('data', function (chunk) \{}
\DoxyCodeLine{    // ...}
\DoxyCodeLine{  \})}
\DoxyCodeLine{\} }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_type_is_README_autotoc_md31234}{}\doxysubsection{typeis.\+is(media\+Type, types)}\label{md_sfcode_node_modules_type_is_README_autotoc_md31234}
Checks if the {\ttfamily media\+Type} is one of the {\ttfamily types}. If the {\ttfamily media\+Type} is invalid or does not matches any of the {\ttfamily types}, then {\ttfamily false} is returned. Otherwise, a string of the type that matched is returned.

The {\ttfamily media\+Type} argument is expected to be a \href{https://tools.ietf.org/html/rfc6838}{\texttt{ media type}} string. The {\ttfamily types} argument is an array of type strings.

Each type in the {\ttfamily types} array can be one of the following\+:


\begin{DoxyItemize}
\item A file extension name such as {\ttfamily json}. This name will be returned if matched.
\item A mime type such as {\ttfamily application/json}.
\item A mime type with a wildcard such as {\ttfamily $\ast$/$\ast$} or {\ttfamily $\ast$/json} or {\ttfamily application/$\ast$}. The full mime type will be returned if matched.
\item A suffix such as {\ttfamily +json}. This can be combined with a wildcard such as {\ttfamily $\ast$/vnd+json} or {\ttfamily application/$\ast$+json}. The full mime type will be returned if matched.
\end{DoxyItemize}

Some examples to illustrate the inputs and returned value\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var mediaType = 'application/json'}
\DoxyCodeLine{}
\DoxyCodeLine{typeis.is(mediaType, ['json']) // => 'json'}
\DoxyCodeLine{typeis.is(mediaType, ['html', 'json']) // => 'json'}
\DoxyCodeLine{typeis.is(mediaType, ['application/*']) // => 'application/json'}
\DoxyCodeLine{typeis.is(mediaType, ['application/json']) // => 'application/json'}
\DoxyCodeLine{}
\DoxyCodeLine{typeis.is(mediaType, ['html']) // => false }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_type_is_README_autotoc_md31235}{}\doxysection{Examples}\label{md_sfcode_node_modules_type_is_README_autotoc_md31235}
\hypertarget{md_sfcode_node_modules_type_is_README_autotoc_md31236}{}\doxysubsection{Example body parser}\label{md_sfcode_node_modules_type_is_README_autotoc_md31236}

\begin{DoxyCode}{0}
\DoxyCodeLine{var express = require('express')}
\DoxyCodeLine{var typeis = require('type-\/is')}
\DoxyCodeLine{}
\DoxyCodeLine{var app = express()}
\DoxyCodeLine{}
\DoxyCodeLine{app.use(function bodyParser (req, res, next) \{}
\DoxyCodeLine{  if (!typeis.hasBody(req)) \{}
\DoxyCodeLine{    return next()}
\DoxyCodeLine{  \}}
\DoxyCodeLine{}
\DoxyCodeLine{  switch (typeis(req, ['urlencoded', 'json', 'multipart'])) \{}
\DoxyCodeLine{    case 'urlencoded':}
\DoxyCodeLine{      // parse urlencoded body}
\DoxyCodeLine{      throw new Error('implement urlencoded body parsing')}
\DoxyCodeLine{    case 'json':}
\DoxyCodeLine{      // parse json body}
\DoxyCodeLine{      throw new Error('implement json body parsing')}
\DoxyCodeLine{    case 'multipart':}
\DoxyCodeLine{      // parse multipart body}
\DoxyCodeLine{      throw new Error('implement multipart body parsing')}
\DoxyCodeLine{    default:}
\DoxyCodeLine{      // 415 error code}
\DoxyCodeLine{      res.statusCode = 415}
\DoxyCodeLine{      res.end()}
\DoxyCodeLine{      break}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\}) }

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