| 1 |
# content-type |
| 2 |
|
| 3 |
[![NPM Version][npm-version-image]][npm-url] |
| 4 |
[![NPM Downloads][npm-downloads-image]][npm-url] |
| 5 |
[![Node.js Version][node-image]][node-url] |
| 6 |
[![Build Status][ci-image]][ci-url] |
| 7 |
[![Coverage Status][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` header. 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({ |
| 66 |
type: 'image/svg+xml', |
| 67 |
parameters: { charset: 'utf-8' } |
| 68 |
}) |
| 69 |
``` |
| 70 |
|
| 71 |
Format an object into a `Content-Type` header. This will return a string of the |
| 72 |
content type for the given object with the following properties (examples are |
| 73 |
shown that produce the string `'image/svg+xml; charset=utf-8'`): |
| 74 |
|
| 75 |
- `type`: The media type (will be lower-cased). Example: `'image/svg+xml'` |
| 76 |
|
| 77 |
- `parameters`: An object of the parameters in the media type (name of the |
| 78 |
parameter will be lower-cased). Example: `{charset: 'utf-8'}` |
| 79 |
|
| 80 |
Throws a `TypeError` if the object contains an invalid type or parameter names. |
| 81 |
|
| 82 |
## License |
| 83 |
|
| 84 |
[MIT](LICENSE) |
| 85 |
|
| 86 |
[ci-image]: https://badgen.net/github/checks/jshttp/content-type/master?label=ci |
| 87 |
[ci-url]: https://github.com/jshttp/content-type/actions/workflows/ci.yml |
| 88 |
[coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/content-type/master |
| 89 |
[coveralls-url]: https://coveralls.io/r/jshttp/content-type?branch=master |
| 90 |
[node-image]: https://badgen.net/npm/node/content-type |
| 91 |
[node-url]: https://nodejs.org/en/download |
| 92 |
[npm-downloads-image]: https://badgen.net/npm/dm/content-type |
| 93 |
[npm-url]: https://npmjs.org/package/content-type |
| 94 |
[npm-version-image]: https://badgen.net/npm/v/content-type |