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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # type-is
  2. [![NPM Version][npm-version-image]][npm-url]
  3. [![NPM Downloads][npm-downloads-image]][npm-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. ### typeis(request, types)
  25. Checks if the `request` is one of the `types`. If the request has no body,
  26. even if there is a `Content-Type` header, then `null` is returned. If the
  27. `Content-Type` header is invalid or does not matches any of the `types`, then
  28. `false` is returned. Otherwise, a string of the type that matched is returned.
  29. The `request` argument is expected to be a Node.js HTTP request. The `types`
  30. argument is an array of type strings.
  31. Each type in the `types` array can be one of the following:
  32. - A file extension name such as `json`. This name will be returned if matched.
  33. - A mime type such as `application/json`.
  34. - A mime type with a wildcard such as `*/*` or `*/json` or `application/*`.
  35. The full mime type will be returned if matched.
  36. - A suffix such as `+json`. This can be combined with a wildcard such as
  37. `*/vnd+json` or `application/*+json`. The full mime type will be returned
  38. if matched.
  39. Some examples to illustrate the inputs and returned value:
  40. <!-- eslint-disable no-undef -->
  41. ```js
  42. // req.headers.content-type = 'application/json'
  43. typeis(req, ['json']) // => 'json'
  44. typeis(req, ['html', 'json']) // => 'json'
  45. typeis(req, ['application/*']) // => 'application/json'
  46. typeis(req, ['application/json']) // => 'application/json'
  47. typeis(req, ['html']) // => false
  48. ```
  49. ### typeis.hasBody(request)
  50. Returns a Boolean if the given `request` has a body, regardless of the
  51. `Content-Type` header.
  52. Having a body has no relation to how large the body is (it may be 0 bytes).
  53. This is similar to how file existence works. If a body does exist, then this
  54. indicates that there is data to read from the Node.js request stream.
  55. <!-- eslint-disable no-undef -->
  56. ```js
  57. if (typeis.hasBody(req)) {
  58. // read the body, since there is one
  59. req.on('data', function (chunk) {
  60. // ...
  61. })
  62. }
  63. ```
  64. ### typeis.is(mediaType, types)
  65. Checks if the `mediaType` is one of the `types`. If the `mediaType` is invalid
  66. or does not matches any of the `types`, then `false` is returned. Otherwise, a
  67. string of the type that matched is returned.
  68. The `mediaType` argument is expected to be a
  69. [media type](https://tools.ietf.org/html/rfc6838) string. The `types` argument
  70. is an array of type strings.
  71. Each type in the `types` array can be one of the following:
  72. - A file extension name such as `json`. This name will be returned if matched.
  73. - A mime type such as `application/json`.
  74. - A mime type with a wildcard such as `*/*` or `*/json` or `application/*`.
  75. The full mime type will be returned if matched.
  76. - A suffix such as `+json`. This can be combined with a wildcard such as
  77. `*/vnd+json` or `application/*+json`. The full mime type will be returned
  78. if matched.
  79. Some examples to illustrate the inputs and returned value:
  80. <!-- eslint-disable no-undef -->
  81. ```js
  82. var mediaType = 'application/json'
  83. typeis.is(mediaType, ['json']) // => 'json'
  84. typeis.is(mediaType, ['html', 'json']) // => 'json'
  85. typeis.is(mediaType, ['application/*']) // => 'application/json'
  86. typeis.is(mediaType, ['application/json']) // => 'application/json'
  87. typeis.is(mediaType, ['html']) // => false
  88. ```
  89. ## Examples
  90. ### Example body parser
  91. ```js
  92. var express = require('express')
  93. var typeis = require('type-is')
  94. var app = express()
  95. app.use(function bodyParser (req, res, next) {
  96. if (!typeis.hasBody(req)) {
  97. return next()
  98. }
  99. switch (typeis(req, ['urlencoded', 'json', 'multipart'])) {
  100. case 'urlencoded':
  101. // parse urlencoded body
  102. throw new Error('implement urlencoded body parsing')
  103. case 'json':
  104. // parse json body
  105. throw new Error('implement json body parsing')
  106. case 'multipart':
  107. // parse multipart body
  108. throw new Error('implement multipart body parsing')
  109. default:
  110. // 415 error code
  111. res.statusCode = 415
  112. res.end()
  113. break
  114. }
  115. })
  116. ```
  117. ## License
  118. [MIT](LICENSE)
  119. [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/type-is/master
  120. [coveralls-url]: https://coveralls.io/r/jshttp/type-is?branch=master
  121. [node-version-image]: https://badgen.net/npm/node/type-is
  122. [node-version-url]: https://nodejs.org/en/download
  123. [npm-downloads-image]: https://badgen.net/npm/dm/type-is
  124. [npm-url]: https://npmjs.org/package/type-is
  125. [npm-version-image]: https://badgen.net/npm/v/type-is
  126. [travis-image]: https://badgen.net/travis/jshttp/type-is/master
  127. [travis-url]: https://travis-ci.org/jshttp/type-is