3 [![NPM Version][npm-image]][npm-url]
4 [![NPM Downloads][downloads-image]][downloads-url]
5 [![Node.js Version][node-version-image]][node-version-url]
6 [![Build Status][travis-image]][travis-url]
7 [![Test Coverage][coveralls-image]][coveralls-url]
9 Infer the content-type of a request.
20 var http = require('http')
21 var is = require('type-is')
23 http.createServer(function (req, res) {
24 var istext = is(req, ['text/*'])
25 res.end('you ' + (istext ? 'sent' : 'did not send') + ' me text')
29 ### type = is(request, types)
31 `request` is the node HTTP request. `types` is an array of types.
34 // req.headers.content-type = 'application/json'
36 is(req, ['json']) // 'json'
37 is(req, ['html', 'json']) // 'json'
38 is(req, ['application/*']) // 'application/json'
39 is(req, ['application/json']) // 'application/json'
41 is(req, ['html']) // false
44 ### type = is.is(mediaType, types)
46 `mediaType` is the [media type](https://tools.ietf.org/html/rfc6838) string. `types` is an array of types.
49 var mediaType = 'application/json'
51 is.is(mediaType, ['json']) // 'json'
52 is.is(mediaType, ['html', 'json']) // 'json'
53 is.is(mediaType, ['application/*']) // 'application/json'
54 is.is(mediaType, ['application/json']) // 'application/json'
56 is.is(mediaType, ['html']) // false
61 - An extension name such as `json`. This name will be returned if matched.
62 - A mime type such as `application/json`.
63 - A mime type with a wildcard such as `*/json` or `application/*`. The full mime type will be returned if matched
64 - A suffix such as `+json`. This can be combined with a wildcard such as `*/vnd+json` or `application/*+json`. The full mime type will be returned if matched.
66 `false` will be returned if no type matches.
68 `null` will be returned if the request does not have a body.
72 #### Example body parser
75 var is = require('type-is');
77 function bodyParser(req, res, next) {
78 if (!is.hasBody(req)) {
82 switch (is(req, ['urlencoded', 'json', 'multipart'])) {
84 // parse urlencoded body
85 throw new Error('implement urlencoded body parsing')
89 throw new Error('implement json body parsing')
92 // parse multipart body
93 throw new Error('implement multipart body parsing')
108 [npm-image]: https://img.shields.io/npm/v/type-is.svg?style=flat
109 [npm-url]: https://npmjs.org/package/type-is
110 [node-version-image]: https://img.shields.io/node/v/type-is.svg?style=flat
111 [node-version-url]: http://nodejs.org/download/
112 [travis-image]: https://img.shields.io/travis/jshttp/type-is.svg?style=flat
113 [travis-url]: https://travis-ci.org/jshttp/type-is
114 [coveralls-image]: https://img.shields.io/coveralls/jshttp/type-is.svg?style=flat
115 [coveralls-url]: https://coveralls.io/r/jshttp/type-is?branch=master
116 [downloads-image]: https://img.shields.io/npm/dm/type-is.svg?style=flat
117 [downloads-url]: https://npmjs.org/package/type-is