991d9dcc472f56d93bf69b65ef6c5d0b1f4ad0a094a812411e75d32adbe9a54ffb3739ddeb71dced591bf2be220740bc17aff7e9087069c0dce94a99b3468f 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # content-type
  2. [![NPM Version][npm-version-image]][npm-url]
  3. [![NPM Downloads][npm-downloads-image]][npm-url]
  4. [![Node.js Version][node-image]][node-url]
  5. [![Build Status][ci-image]][ci-url]
  6. [![Coverage Status][coveralls-image]][coveralls-url]
  7. Create and parse HTTP Content-Type header according to RFC 7231
  8. ## Installation
  9. ```sh
  10. $ npm install content-type
  11. ```
  12. ## API
  13. ```js
  14. var contentType = require('content-type')
  15. ```
  16. ### contentType.parse(string)
  17. ```js
  18. var obj = contentType.parse('image/svg+xml; charset=utf-8')
  19. ```
  20. Parse a `Content-Type` header. This will return an object with the following
  21. properties (examples are shown for the string `'image/svg+xml; charset=utf-8'`):
  22. - `type`: The media type (the type and subtype, always lower case).
  23. Example: `'image/svg+xml'`
  24. - `parameters`: An object of the parameters in the media type (name of parameter
  25. always lower case). Example: `{charset: 'utf-8'}`
  26. Throws a `TypeError` if the string is missing or invalid.
  27. ### contentType.parse(req)
  28. ```js
  29. var obj = contentType.parse(req)
  30. ```
  31. Parse the `Content-Type` header from the given `req`. Short-cut for
  32. `contentType.parse(req.headers['content-type'])`.
  33. Throws a `TypeError` if the `Content-Type` header is missing or invalid.
  34. ### contentType.parse(res)
  35. ```js
  36. var obj = contentType.parse(res)
  37. ```
  38. Parse the `Content-Type` header set on the given `res`. Short-cut for
  39. `contentType.parse(res.getHeader('content-type'))`.
  40. Throws a `TypeError` if the `Content-Type` header is missing or invalid.
  41. ### contentType.format(obj)
  42. ```js
  43. var str = contentType.format({
  44. type: 'image/svg+xml',
  45. parameters: { charset: 'utf-8' }
  46. })
  47. ```
  48. Format an object into a `Content-Type` header. This will return a string of the
  49. content type for the given object with the following properties (examples are
  50. shown that produce the string `'image/svg+xml; charset=utf-8'`):
  51. - `type`: The media type (will be lower-cased). Example: `'image/svg+xml'`
  52. - `parameters`: An object of the parameters in the media type (name of the
  53. parameter will be lower-cased). Example: `{charset: 'utf-8'}`
  54. Throws a `TypeError` if the object contains an invalid type or parameter names.
  55. ## License
  56. [MIT](LICENSE)
  57. [ci-image]: https://badgen.net/github/checks/jshttp/content-type/master?label=ci
  58. [ci-url]: https://github.com/jshttp/content-type/actions/workflows/ci.yml
  59. [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/content-type/master
  60. [coveralls-url]: https://coveralls.io/r/jshttp/content-type?branch=master
  61. [node-image]: https://badgen.net/npm/node/content-type
  62. [node-url]: https://nodejs.org/en/download
  63. [npm-downloads-image]: https://badgen.net/npm/dm/content-type
  64. [npm-url]: https://npmjs.org/package/content-type
  65. [npm-version-image]: https://badgen.net/npm/v/content-type