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]
9 Create and parse HTTP Content-Type header according to RFC 7231
14 $ npm install content-type
20 var contentType = require('content-type')
23 ### contentType.parse(string)
26 var obj = contentType.parse('image/svg+xml; charset=utf-8')
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'`):
32 - `type`: The media type (the type and subtype, always lower case).
33 Example: `'image/svg+xml'`
35 - `parameters`: An object of the parameters in the media type (name of parameter
36 always lower case). Example: `{charset: 'utf-8'}`
38 Throws a `TypeError` if the string is missing or invalid.
40 ### contentType.parse(req)
43 var obj = contentType.parse(req)
46 Parse the `content-type` header from the given `req`. Short-cut for
47 `contentType.parse(req.headers['content-type'])`.
49 Throws a `TypeError` if the `Content-Type` header is missing or invalid.
51 ### contentType.parse(res)
54 var obj = contentType.parse(res)
57 Parse the `content-type` header set on the given `res`. Short-cut for
58 `contentType.parse(res.getHeader('content-type'))`.
60 Throws a `TypeError` if the `Content-Type` header is missing or invalid.
62 ### contentType.format(obj)
65 var str = contentType.format({type: 'image/svg+xml'})
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'`):
72 - `type`: The media type (will be lower-cased). Example: `'image/svg+xml'`
74 - `parameters`: An object of the parameters in the media type (name of the
75 parameter will be lower-cased). Example: `{charset: 'utf-8'}`
77 Throws a `TypeError` if the object contains an invalid type or parameter names.
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