[e29cc2e] | 1 | # mime
|
---|
[6a3a178] | 2 |
|
---|
[e29cc2e] | 3 | Comprehensive MIME type mapping API based on mime-db module.
|
---|
[6a3a178] | 4 |
|
---|
| 5 | ## Install
|
---|
| 6 |
|
---|
[e29cc2e] | 7 | Install with [npm](http://github.com/isaacs/npm):
|
---|
[6a3a178] | 8 |
|
---|
[e29cc2e] | 9 | npm install mime
|
---|
[6a3a178] | 10 |
|
---|
[e29cc2e] | 11 | ## Contributing / Testing
|
---|
[6a3a178] | 12 |
|
---|
[e29cc2e] | 13 | npm run test
|
---|
[6a3a178] | 14 |
|
---|
[e29cc2e] | 15 | ## Command Line
|
---|
[6a3a178] | 16 |
|
---|
[e29cc2e] | 17 | mime [path_string]
|
---|
[6a3a178] | 18 |
|
---|
[e29cc2e] | 19 | E.g.
|
---|
[6a3a178] | 20 |
|
---|
[e29cc2e] | 21 | > mime scripts/jquery.js
|
---|
| 22 | application/javascript
|
---|
[6a3a178] | 23 |
|
---|
[e29cc2e] | 24 | ## API - Queries
|
---|
[6a3a178] | 25 |
|
---|
[e29cc2e] | 26 | ### mime.lookup(path)
|
---|
| 27 | Get the mime type associated with a file, if no mime type is found `application/octet-stream` is returned. Performs a case-insensitive lookup using the extension in `path` (the substring after the last '/' or '.'). E.g.
|
---|
[6a3a178] | 28 |
|
---|
[e29cc2e] | 29 | ```js
|
---|
| 30 | var mime = require('mime');
|
---|
[6a3a178] | 31 |
|
---|
[e29cc2e] | 32 | mime.lookup('/path/to/file.txt'); // => 'text/plain'
|
---|
| 33 | mime.lookup('file.txt'); // => 'text/plain'
|
---|
| 34 | mime.lookup('.TXT'); // => 'text/plain'
|
---|
| 35 | mime.lookup('htm'); // => 'text/html'
|
---|
[6a3a178] | 36 | ```
|
---|
| 37 |
|
---|
[e29cc2e] | 38 | ### mime.default_type
|
---|
| 39 | Sets the mime type returned when `mime.lookup` fails to find the extension searched for. (Default is `application/octet-stream`.)
|
---|
[6a3a178] | 40 |
|
---|
[e29cc2e] | 41 | ### mime.extension(type)
|
---|
| 42 | Get the default extension for `type`
|
---|
[6a3a178] | 43 |
|
---|
[e29cc2e] | 44 | ```js
|
---|
| 45 | mime.extension('text/html'); // => 'html'
|
---|
| 46 | mime.extension('application/octet-stream'); // => 'bin'
|
---|
| 47 | ```
|
---|
[6a3a178] | 48 |
|
---|
[e29cc2e] | 49 | ### mime.charsets.lookup()
|
---|
[6a3a178] | 50 |
|
---|
[e29cc2e] | 51 | Map mime-type to charset
|
---|
[6a3a178] | 52 |
|
---|
[e29cc2e] | 53 | ```js
|
---|
| 54 | mime.charsets.lookup('text/plain'); // => 'UTF-8'
|
---|
[6a3a178] | 55 | ```
|
---|
| 56 |
|
---|
[e29cc2e] | 57 | (The logic for charset lookups is pretty rudimentary. Feel free to suggest improvements.)
|
---|
[6a3a178] | 58 |
|
---|
[e29cc2e] | 59 | ## API - Defining Custom Types
|
---|
[6a3a178] | 60 |
|
---|
[e29cc2e] | 61 | Custom type mappings can be added on a per-project basis via the following APIs.
|
---|
[6a3a178] | 62 |
|
---|
[e29cc2e] | 63 | ### mime.define()
|
---|
[6a3a178] | 64 |
|
---|
[e29cc2e] | 65 | Add custom mime/extension mappings
|
---|
[6a3a178] | 66 |
|
---|
[e29cc2e] | 67 | ```js
|
---|
| 68 | mime.define({
|
---|
| 69 | 'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'],
|
---|
| 70 | 'application/x-my-type': ['x-mt', 'x-mtt'],
|
---|
| 71 | // etc ...
|
---|
| 72 | });
|
---|
[6a3a178] | 73 |
|
---|
[e29cc2e] | 74 | mime.lookup('x-sft'); // => 'text/x-some-format'
|
---|
[6a3a178] | 75 | ```
|
---|
| 76 |
|
---|
[e29cc2e] | 77 | The first entry in the extensions array is returned by `mime.extension()`. E.g.
|
---|
[6a3a178] | 78 |
|
---|
[e29cc2e] | 79 | ```js
|
---|
| 80 | mime.extension('text/x-some-format'); // => 'x-sf'
|
---|
[6a3a178] | 81 | ```
|
---|
| 82 |
|
---|
[e29cc2e] | 83 | ### mime.load(filepath)
|
---|
[6a3a178] | 84 |
|
---|
[e29cc2e] | 85 | Load mappings from an Apache ".types" format file
|
---|
[6a3a178] | 86 |
|
---|
[e29cc2e] | 87 | ```js
|
---|
| 88 | mime.load('./my_project.types');
|
---|
[6a3a178] | 89 | ```
|
---|
[e29cc2e] | 90 | The .types file format is simple - See the `types` dir for examples.
|
---|