1 | # mime-types
|
2 |
|
3 | [![NPM Version][npm-version-image]][npm-url]
|
4 | [![NPM Downloads][npm-downloads-image]][npm-url]
|
5 | [![Node.js Version][node-version-image]][node-version-url]
|
6 | [![Build Status][ci-image]][ci-url]
|
7 | [![Test Coverage][coveralls-image]][coveralls-url]
|
8 |
|
9 | The ultimate javascript content-type utility.
|
10 |
|
11 | Similar to [the `mime@1.x` module](https://www.npmjs.com/package/mime), except:
|
12 |
|
13 | - __No fallbacks.__ Instead of naively returning the first available type,
|
14 | `mime-types` simply returns `false`, so do
|
15 | `var type = mime.lookup('unrecognized') || 'application/octet-stream'`.
|
16 | - No `new Mime()` business, so you could do `var lookup = require('mime-types').lookup`.
|
17 | - No `.define()` functionality
|
18 | - Bug fixes for `.lookup(path)`
|
19 |
|
20 | Otherwise, the API is compatible with `mime` 1.x.
|
21 |
|
22 | ## Install
|
23 |
|
24 | This is a [Node.js](https://nodejs.org/en/) module available through the
|
25 | [npm registry](https://www.npmjs.com/). Installation is done using the
|
26 | [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):
|
27 |
|
28 | ```sh
|
29 | $ npm install mime-types
|
30 | ```
|
31 |
|
32 | ## Adding Types
|
33 |
|
34 | All mime types are based on [mime-db](https://www.npmjs.com/package/mime-db),
|
35 | so open a PR there if you'd like to add mime types.
|
36 |
|
37 | ## API
|
38 |
|
39 | ```js
|
40 | var mime = require('mime-types')
|
41 | ```
|
42 |
|
43 | All functions return `false` if input is invalid or not found.
|
44 |
|
45 | ### mime.lookup(path)
|
46 |
|
47 | Lookup the content-type associated with a file.
|
48 |
|
49 | ```js
|
50 | mime.lookup('json') // 'application/json'
|
51 | mime.lookup('.md') // 'text/markdown'
|
52 | mime.lookup('file.html') // 'text/html'
|
53 | mime.lookup('folder/file.js') // 'application/javascript'
|
54 | mime.lookup('folder/.htaccess') // false
|
55 |
|
56 | mime.lookup('cats') // false
|
57 | ```
|
58 |
|
59 | ### mime.contentType(type)
|
60 |
|
61 | Create a full content-type header given a content-type or extension.
|
62 | When given an extension, `mime.lookup` is used to get the matching
|
63 | content-type, otherwise the given content-type is used. Then if the
|
64 | content-type does not already have a `charset` parameter, `mime.charset`
|
65 | is used to get the default charset and add to the returned content-type.
|
66 |
|
67 | ```js
|
68 | mime.contentType('markdown') // 'text/x-markdown; charset=utf-8'
|
69 | mime.contentType('file.json') // 'application/json; charset=utf-8'
|
70 | mime.contentType('text/html') // 'text/html; charset=utf-8'
|
71 | mime.contentType('text/html; charset=iso-8859-1') // 'text/html; charset=iso-8859-1'
|
72 |
|
73 | // from a full path
|
74 | mime.contentType(path.extname('/path/to/file.json')) // 'application/json; charset=utf-8'
|
75 | ```
|
76 |
|
77 | ### mime.extension(type)
|
78 |
|
79 | Get the default extension for a content-type.
|
80 |
|
81 | ```js
|
82 | mime.extension('application/octet-stream') // 'bin'
|
83 | ```
|
84 |
|
85 | ### mime.charset(type)
|
86 |
|
87 | Lookup the implied default charset of a content-type.
|
88 |
|
89 | ```js
|
90 | mime.charset('text/markdown') // 'UTF-8'
|
91 | ```
|
92 |
|
93 | ### var type = mime.types[extension]
|
94 |
|
95 | A map of content-types by extension.
|
96 |
|
97 | ### [extensions...] = mime.extensions[type]
|
98 |
|
99 | A map of extensions by content-type.
|
100 |
|
101 | ## License
|
102 |
|
103 | [MIT](LICENSE)
|
104 |
|
105 | [ci-image]: https://badgen.net/github/checks/jshttp/mime-types/master?label=ci
|
106 | [ci-url]: https://github.com/jshttp/mime-types/actions/workflows/ci.yml
|
107 | [coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/mime-types/master
|
108 | [coveralls-url]: https://coveralls.io/r/jshttp/mime-types?branch=master
|
109 | [node-version-image]: https://badgen.net/npm/node/mime-types
|
110 | [node-version-url]: https://nodejs.org/en/download
|
111 | [npm-downloads-image]: https://badgen.net/npm/dm/mime-types
|
112 | [npm-url]: https://npmjs.org/package/mime-types
|
113 | [npm-version-image]: https://badgen.net/npm/v/mime-types
|