You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.1 KiB

7 years ago
  1. # mime
  2. Comprehensive MIME type mapping API based on mime-db module.
  3. ## Install
  4. Install with [npm](http://github.com/isaacs/npm):
  5. npm install mime
  6. ## Contributing / Testing
  7. npm run test
  8. ## Command Line
  9. mime [path_string]
  10. E.g.
  11. > mime scripts/jquery.js
  12. application/javascript
  13. ## API - Queries
  14. ### mime.lookup(path)
  15. 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.
  16. ```js
  17. var mime = require('mime');
  18. mime.lookup('/path/to/file.txt'); // => 'text/plain'
  19. mime.lookup('file.txt'); // => 'text/plain'
  20. mime.lookup('.TXT'); // => 'text/plain'
  21. mime.lookup('htm'); // => 'text/html'
  22. ```
  23. ### mime.default_type
  24. Sets the mime type returned when `mime.lookup` fails to find the extension searched for. (Default is `application/octet-stream`.)
  25. ### mime.extension(type)
  26. Get the default extension for `type`
  27. ```js
  28. mime.extension('text/html'); // => 'html'
  29. mime.extension('application/octet-stream'); // => 'bin'
  30. ```
  31. ### mime.charsets.lookup()
  32. Map mime-type to charset
  33. ```js
  34. mime.charsets.lookup('text/plain'); // => 'UTF-8'
  35. ```
  36. (The logic for charset lookups is pretty rudimentary. Feel free to suggest improvements.)
  37. ## API - Defining Custom Types
  38. Custom type mappings can be added on a per-project basis via the following APIs.
  39. ### mime.define()
  40. Add custom mime/extension mappings
  41. ```js
  42. mime.define({
  43. 'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'],
  44. 'application/x-my-type': ['x-mt', 'x-mtt'],
  45. // etc ...
  46. });
  47. mime.lookup('x-sft'); // => 'text/x-some-format'
  48. ```
  49. The first entry in the extensions array is returned by `mime.extension()`. E.g.
  50. ```js
  51. mime.extension('text/x-some-format'); // => 'x-sf'
  52. ```
  53. ### mime.load(filepath)
  54. Load mappings from an Apache ".types" format file
  55. ```js
  56. mime.load('./my_project.types');
  57. ```
  58. The .types file format is simple - See the `types` dir for examples.