UNPKG

3.63 kBMarkdownView Raw
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][travis-image]][travis-url]
7[![Test Coverage][coveralls-image]][coveralls-url]
8
9The ultimate javascript content-type utility.
10
11Similar 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
20Otherwise, the API is compatible with `mime` 1.x.
21
22## Install
23
24This 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
34All mime types are based on [mime-db](https://www.npmjs.com/package/mime-db),
35so open a PR there if you'd like to add mime types.
36
37## API
38
39<!-- eslint-disable no-unused-vars -->
40
41```js
42var mime = require('mime-types')
43```
44
45All functions return `false` if input is invalid or not found.
46
47### mime.lookup(path)
48
49Lookup the content-type associated with a file.
50
51<!-- eslint-disable no-undef -->
52
53```js
54mime.lookup('json') // 'application/json'
55mime.lookup('.md') // 'text/markdown'
56mime.lookup('file.html') // 'text/html'
57mime.lookup('folder/file.js') // 'application/javascript'
58mime.lookup('folder/.htaccess') // false
59
60mime.lookup('cats') // false
61```
62
63### mime.contentType(type)
64
65Create a full content-type header given a content-type or extension.
66When given an extension, `mime.lookup` is used to get the matching
67content-type, otherwise the given content-type is used. Then if the
68content-type does not already have a `charset` parameter, `mime.charset`
69is used to get the default charset and add to the returned content-type.
70
71<!-- eslint-disable no-undef -->
72
73```js
74mime.contentType('markdown') // 'text/x-markdown; charset=utf-8'
75mime.contentType('file.json') // 'application/json; charset=utf-8'
76mime.contentType('text/html') // 'text/html; charset=utf-8'
77mime.contentType('text/html; charset=iso-8859-1') // 'text/html; charset=iso-8859-1'
78
79// from a full path
80mime.contentType(path.extname('/path/to/file.json')) // 'application/json; charset=utf-8'
81```
82
83### mime.extension(type)
84
85Get the default extension for a content-type.
86
87<!-- eslint-disable no-undef -->
88
89```js
90mime.extension('application/octet-stream') // 'bin'
91```
92
93### mime.charset(type)
94
95Lookup the implied default charset of a content-type.
96
97<!-- eslint-disable no-undef -->
98
99```js
100mime.charset('text/markdown') // 'UTF-8'
101```
102
103### var type = mime.types[extension]
104
105A map of content-types by extension.
106
107### [extensions...] = mime.extensions[type]
108
109A map of extensions by content-type.
110
111## License
112
113[MIT](LICENSE)
114
115[coveralls-image]: https://badgen.net/coveralls/c/github/jshttp/mime-types/master
116[coveralls-url]: https://coveralls.io/r/jshttp/mime-types?branch=master
117[node-version-image]: https://badgen.net/npm/node/mime-types
118[node-version-url]: https://nodejs.org/en/download
119[npm-downloads-image]: https://badgen.net/npm/dm/mime-types
120[npm-url]: https://npmjs.org/package/mime-types
121[npm-version-image]: https://badgen.net/npm/v/mime-types
122[travis-image]: https://badgen.net/travis/jshttp/mime-types/master
123[travis-url]: https://travis-ci.org/jshttp/mime-types