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 ### is.hasBody(request)
46 Returns a Boolean if the given `request` has a body, regardless of the
47 `Content-Type` header.
49 Having a body has no relation to how large the body is (it may be 0 bytes).
50 This is similar to how file existence works. If a body does exist, then this
51 indicates that there is data to read from the Node.js request stream.
54 if (is.hasBody(req)) {
55 // read the body, since there is one
57 req.on('data', function (chunk) {
63 ### type = is.is(mediaType, types)
65 `mediaType` is the [media type](https://tools.ietf.org/html/rfc6838) string. `types` is an array of types.
68 var mediaType = 'application/json'
70 is.is(mediaType, ['json']) // 'json'
71 is.is(mediaType, ['html', 'json']) // 'json'
72 is.is(mediaType, ['application/*']) // 'application/json'
73 is.is(mediaType, ['application/json']) // 'application/json'
75 is.is(mediaType, ['html']) // false
80 - An extension name such as `json`. This name will be returned if matched.
81 - A mime type such as `application/json`.
82 - A mime type with a wildcard such as `*/*` or `*/json` or `application/*`. The full mime type will be returned if matched.
83 - 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.
85 `false` will be returned if no type matches or the content type is invalid.
87 `null` will be returned if the request does not have a body.
91 #### Example body parser
94 var is = require('type-is');
96 function bodyParser(req, res, next) {
97 if (!is.hasBody(req)) {
101 switch (is(req, ['urlencoded', 'json', 'multipart'])) {
103 // parse urlencoded body
104 throw new Error('implement urlencoded body parsing')
108 throw new Error('implement json body parsing')
111 // parse multipart body
112 throw new Error('implement multipart body parsing')
127 [npm-image]: https://img.shields.io/npm/v/type-is.svg
128 [npm-url]: https://npmjs.org/package/type-is
129 [node-version-image]: https://img.shields.io/node/v/type-is.svg
130 [node-version-url]: https://nodejs.org/en/download/
131 [travis-image]: https://img.shields.io/travis/jshttp/type-is/master.svg
132 [travis-url]: https://travis-ci.org/jshttp/type-is
133 [coveralls-image]: https://img.shields.io/coveralls/jshttp/type-is/master.svg
134 [coveralls-url]: https://coveralls.io/r/jshttp/type-is?branch=master
135 [downloads-image]: https://img.shields.io/npm/dm/type-is.svg
136 [downloads-url]: https://npmjs.org/package/type-is