arnaucube
arnaucube/socketioMEANseedPublic Self-hosted repository
socketioMEANseed/server/node_modules/type-is/README.md
server/node_modules/type-is/README.mdRaw
  1# type-is
  2
  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]
  8
  9Infer the content-type of a request.
 10
 11### Install
 12
 13```sh
 14$ npm install type-is
 15```
 16
 17## API
 18
 19```js
 20var http = require('http')
 21var is   = require('type-is')
 22
 23http.createServer(function (req, res) {
 24  var istext = is(req, ['text/*'])
 25  res.end('you ' + (istext ? 'sent' : 'did not send') + ' me text')
 26})
 27```
 28
 29### type = is(request, types)
 30
 31`request` is the node HTTP request. `types` is an array of types.
 32
 33```js
 34// req.headers.content-type = 'application/json'
 35
 36is(req, ['json'])             // 'json'
 37is(req, ['html', 'json'])     // 'json'
 38is(req, ['application/*'])    // 'application/json'
 39is(req, ['application/json']) // 'application/json'
 40
 41is(req, ['html']) // false
 42```
 43
 44### type = is.is(mediaType, types)
 45
 46`mediaType` is the [media type](https://tools.ietf.org/html/rfc6838) string. `types` is an array of types.
 47
 48```js
 49var mediaType = 'application/json'
 50
 51is.is(mediaType, ['json'])             // 'json'
 52is.is(mediaType, ['html', 'json'])     // 'json'
 53is.is(mediaType, ['application/*'])    // 'application/json'
 54is.is(mediaType, ['application/json']) // 'application/json'
 55
 56is.is(mediaType, ['html']) // false
 57```
 58
 59### Each type can be:
 60
 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.
 65
 66`false` will be returned if no type matches.
 67
 68`null` will be returned if the request does not have a body.
 69
 70## Examples
 71
 72#### Example body parser
 73
 74```js
 75var is = require('type-is');
 76
 77function bodyParser(req, res, next) {
 78  if (!is.hasBody(req)) {
 79    return next()
 80  }
 81
 82  switch (is(req, ['urlencoded', 'json', 'multipart'])) {
 83    case 'urlencoded':
 84      // parse urlencoded body
 85      throw new Error('implement urlencoded body parsing')
 86      break
 87    case 'json':
 88      // parse json body
 89      throw new Error('implement json body parsing')
 90      break
 91    case 'multipart':
 92      // parse multipart body
 93      throw new Error('implement multipart body parsing')
 94      break
 95    default:
 96      // 415 error code
 97      res.statusCode = 415
 98      res.end()
 99      return
100  }
101}
102```
103
104## License
105
106[MIT](LICENSE)
107
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