[d24f17c] | 1 | # mime
|
---|
| 2 |
|
---|
| 3 | Comprehensive MIME type mapping API based on mime-db module.
|
---|
| 4 |
|
---|
| 5 | ## Install
|
---|
| 6 |
|
---|
| 7 | Install with [npm](http://github.com/isaacs/npm):
|
---|
| 8 |
|
---|
| 9 | npm install mime
|
---|
| 10 |
|
---|
| 11 | ## Contributing / Testing
|
---|
| 12 |
|
---|
| 13 | npm run test
|
---|
| 14 |
|
---|
| 15 | ## Command Line
|
---|
| 16 |
|
---|
| 17 | mime [path_string]
|
---|
| 18 |
|
---|
| 19 | E.g.
|
---|
| 20 |
|
---|
| 21 | > mime scripts/jquery.js
|
---|
| 22 | application/javascript
|
---|
| 23 |
|
---|
| 24 | ## API - Queries
|
---|
| 25 |
|
---|
| 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.
|
---|
| 28 |
|
---|
| 29 | ```js
|
---|
| 30 | var mime = require('mime');
|
---|
| 31 |
|
---|
| 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'
|
---|
| 36 | ```
|
---|
| 37 |
|
---|
| 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`.)
|
---|
| 40 |
|
---|
| 41 | ### mime.extension(type)
|
---|
| 42 | Get the default extension for `type`
|
---|
| 43 |
|
---|
| 44 | ```js
|
---|
| 45 | mime.extension('text/html'); // => 'html'
|
---|
| 46 | mime.extension('application/octet-stream'); // => 'bin'
|
---|
| 47 | ```
|
---|
| 48 |
|
---|
| 49 | ### mime.charsets.lookup()
|
---|
| 50 |
|
---|
| 51 | Map mime-type to charset
|
---|
| 52 |
|
---|
| 53 | ```js
|
---|
| 54 | mime.charsets.lookup('text/plain'); // => 'UTF-8'
|
---|
| 55 | ```
|
---|
| 56 |
|
---|
| 57 | (The logic for charset lookups is pretty rudimentary. Feel free to suggest improvements.)
|
---|
| 58 |
|
---|
| 59 | ## API - Defining Custom Types
|
---|
| 60 |
|
---|
| 61 | Custom type mappings can be added on a per-project basis via the following APIs.
|
---|
| 62 |
|
---|
| 63 | ### mime.define()
|
---|
| 64 |
|
---|
| 65 | Add custom mime/extension mappings
|
---|
| 66 |
|
---|
| 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 | });
|
---|
| 73 |
|
---|
| 74 | mime.lookup('x-sft'); // => 'text/x-some-format'
|
---|
| 75 | ```
|
---|
| 76 |
|
---|
| 77 | The first entry in the extensions array is returned by `mime.extension()`. E.g.
|
---|
| 78 |
|
---|
| 79 | ```js
|
---|
| 80 | mime.extension('text/x-some-format'); // => 'x-sf'
|
---|
| 81 | ```
|
---|
| 82 |
|
---|
| 83 | ### mime.load(filepath)
|
---|
| 84 |
|
---|
| 85 | Load mappings from an Apache ".types" format file
|
---|
| 86 |
|
---|
| 87 | ```js
|
---|
| 88 | mime.load('./my_project.types');
|
---|
| 89 | ```
|
---|
| 90 | The .types file format is simple - See the `types` dir for examples.
|
---|