[6a3a178] | 1 | # content-type
|
---|
| 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 |
|
---|
| 9 | Create and parse HTTP Content-Type header according to RFC 7231
|
---|
| 10 |
|
---|
| 11 | ## Installation
|
---|
| 12 |
|
---|
| 13 | ```sh
|
---|
| 14 | $ npm install content-type
|
---|
| 15 | ```
|
---|
| 16 |
|
---|
| 17 | ## API
|
---|
| 18 |
|
---|
| 19 | ```js
|
---|
| 20 | var contentType = require('content-type')
|
---|
| 21 | ```
|
---|
| 22 |
|
---|
| 23 | ### contentType.parse(string)
|
---|
| 24 |
|
---|
| 25 | ```js
|
---|
| 26 | var obj = contentType.parse('image/svg+xml; charset=utf-8')
|
---|
| 27 | ```
|
---|
| 28 |
|
---|
| 29 | Parse a content type string. This will return an object with the following
|
---|
| 30 | properties (examples are shown for the string `'image/svg+xml; charset=utf-8'`):
|
---|
| 31 |
|
---|
| 32 | - `type`: The media type (the type and subtype, always lower case).
|
---|
| 33 | Example: `'image/svg+xml'`
|
---|
| 34 |
|
---|
| 35 | - `parameters`: An object of the parameters in the media type (name of parameter
|
---|
| 36 | always lower case). Example: `{charset: 'utf-8'}`
|
---|
| 37 |
|
---|
| 38 | Throws a `TypeError` if the string is missing or invalid.
|
---|
| 39 |
|
---|
| 40 | ### contentType.parse(req)
|
---|
| 41 |
|
---|
| 42 | ```js
|
---|
| 43 | var obj = contentType.parse(req)
|
---|
| 44 | ```
|
---|
| 45 |
|
---|
| 46 | Parse the `content-type` header from the given `req`. Short-cut for
|
---|
| 47 | `contentType.parse(req.headers['content-type'])`.
|
---|
| 48 |
|
---|
| 49 | Throws a `TypeError` if the `Content-Type` header is missing or invalid.
|
---|
| 50 |
|
---|
| 51 | ### contentType.parse(res)
|
---|
| 52 |
|
---|
| 53 | ```js
|
---|
| 54 | var obj = contentType.parse(res)
|
---|
| 55 | ```
|
---|
| 56 |
|
---|
| 57 | Parse the `content-type` header set on the given `res`. Short-cut for
|
---|
| 58 | `contentType.parse(res.getHeader('content-type'))`.
|
---|
| 59 |
|
---|
| 60 | Throws a `TypeError` if the `Content-Type` header is missing or invalid.
|
---|
| 61 |
|
---|
| 62 | ### contentType.format(obj)
|
---|
| 63 |
|
---|
| 64 | ```js
|
---|
| 65 | var str = contentType.format({type: 'image/svg+xml'})
|
---|
| 66 | ```
|
---|
| 67 |
|
---|
| 68 | Format an object into a content type string. This will return a string of the
|
---|
| 69 | content type for the given object with the following properties (examples are
|
---|
| 70 | shown that produce the string `'image/svg+xml; charset=utf-8'`):
|
---|
| 71 |
|
---|
| 72 | - `type`: The media type (will be lower-cased). Example: `'image/svg+xml'`
|
---|
| 73 |
|
---|
| 74 | - `parameters`: An object of the parameters in the media type (name of the
|
---|
| 75 | parameter will be lower-cased). Example: `{charset: 'utf-8'}`
|
---|
| 76 |
|
---|
| 77 | Throws a `TypeError` if the object contains an invalid type or parameter names.
|
---|
| 78 |
|
---|
| 79 | ## License
|
---|
| 80 |
|
---|
| 81 | [MIT](LICENSE)
|
---|
| 82 |
|
---|
| 83 | [npm-image]: https://img.shields.io/npm/v/content-type.svg
|
---|
| 84 | [npm-url]: https://npmjs.org/package/content-type
|
---|
| 85 | [node-version-image]: https://img.shields.io/node/v/content-type.svg
|
---|
| 86 | [node-version-url]: http://nodejs.org/download/
|
---|
| 87 | [travis-image]: https://img.shields.io/travis/jshttp/content-type/master.svg
|
---|
| 88 | [travis-url]: https://travis-ci.org/jshttp/content-type
|
---|
| 89 | [coveralls-image]: https://img.shields.io/coveralls/jshttp/content-type/master.svg
|
---|
| 90 | [coveralls-url]: https://coveralls.io/r/jshttp/content-type
|
---|
| 91 | [downloads-image]: https://img.shields.io/npm/dm/content-type.svg
|
---|
| 92 | [downloads-url]: https://npmjs.org/package/content-type
|
---|