Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # type-is
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Node.js Version][node-version-image]][node-version-url]
  5. [![Build Status][travis-image]][travis-url]
  6. [![Test Coverage][coveralls-image]][coveralls-url]
  7. Infer the content-type of a request.
  8. ### Install
  9. This is a [Node.js](https://nodejs.org/en/) module available through the
  10. [npm registry](https://www.npmjs.com/). Installation is done using the
  11. [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
  12. ```sh
  13. $ npm install type-is
  14. ```
  15. ## API
  16. ```js
  17. var http = require('http')
  18. var typeis = require('type-is')
  19. http.createServer(function (req, res) {
  20. var istext = typeis(req, ['text/*'])
  21. res.end('you ' + (istext ? 'sent' : 'did not send') + ' me text')
  22. })
  23. ```
  24. ### type = typeis(request, types)
  25. `request` is the node HTTP request. `types` is an array of types.
  26. <!-- eslint-disable no-undef -->
  27. ```js
  28. // req.headers.content-type = 'application/json'
  29. typeis(req, ['json']) // 'json'
  30. typeis(req, ['html', 'json']) // 'json'
  31. typeis(req, ['application/*']) // 'application/json'
  32. typeis(req, ['application/json']) // 'application/json'
  33. typeis(req, ['html']) // false
  34. ```
  35. ### typeis.hasBody(request)
  36. Returns a Boolean if the given `request` has a body, regardless of the
  37. `Content-Type` header.
  38. Having a body has no relation to how large the body is (it may be 0 bytes).
  39. This is similar to how file existence works. If a body does exist, then this
  40. indicates that there is data to read from the Node.js request stream.
  41. <!-- eslint-disable no-undef -->
  42. ```js
  43. if (typeis.hasBody(req)) {
  44. // read the body, since there is one
  45. req.on('data', function (chunk) {
  46. // ...
  47. })
  48. }
  49. ```
  50. ### type = typeis.is(mediaType, types)
  51. `mediaType` is the [media type](https://tools.ietf.org/html/rfc6838) string. `types` is an array of types.
  52. <!-- eslint-disable no-undef -->
  53. ```js
  54. var mediaType = 'application/json'
  55. typeis.is(mediaType, ['json']) // 'json'
  56. typeis.is(mediaType, ['html', 'json']) // 'json'
  57. typeis.is(mediaType, ['application/*']) // 'application/json'
  58. typeis.is(mediaType, ['application/json']) // 'application/json'
  59. typeis.is(mediaType, ['html']) // false
  60. ```
  61. ### Each type can be:
  62. - An extension name such as `json`. This name will be returned if matched.
  63. - A mime type such as `application/json`.
  64. - A mime type with a wildcard such as `*/*` or `*/json` or `application/*`. The full mime type will be returned if matched.
  65. - 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 or the content type is invalid.
  67. `null` will be returned if the request does not have a body.
  68. ## Examples
  69. ### Example body parser
  70. ```js
  71. var express = require('express')
  72. var typeis = require('type-is')
  73. var app = express()
  74. app.use(function bodyParser (req, res, next) {
  75. if (!typeis.hasBody(req)) {
  76. return next()
  77. }
  78. switch (typeis(req, ['urlencoded', 'json', 'multipart'])) {
  79. case 'urlencoded':
  80. // parse urlencoded body
  81. throw new Error('implement urlencoded body parsing')
  82. case 'json':
  83. // parse json body
  84. throw new Error('implement json body parsing')
  85. case 'multipart':
  86. // parse multipart body
  87. throw new Error('implement multipart body parsing')
  88. default:
  89. // 415 error code
  90. res.statusCode = 415
  91. res.end()
  92. break
  93. }
  94. })
  95. ```
  96. ## License
  97. [MIT](LICENSE)
  98. [npm-image]: https://img.shields.io/npm/v/type-is.svg
  99. [npm-url]: https://npmjs.org/package/type-is
  100. [node-version-image]: https://img.shields.io/node/v/type-is.svg
  101. [node-version-url]: https://nodejs.org/en/download/
  102. [travis-image]: https://img.shields.io/travis/jshttp/type-is/master.svg
  103. [travis-url]: https://travis-ci.org/jshttp/type-is
  104. [coveralls-image]: https://img.shields.io/coveralls/jshttp/type-is/master.svg
  105. [coveralls-url]: https://coveralls.io/r/jshttp/type-is?branch=master
  106. [downloads-image]: https://img.shields.io/npm/dm/type-is.svg
  107. [downloads-url]: https://npmjs.org/package/type-is