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.

66 lines
2.2 KiB

8 years ago
  1. # mime
  2. Comprehensive MIME type mapping API. Includes all 600+ types and 800+ extensions defined by the Apache project, plus additional types submitted by the node.js community.
  3. ## Install
  4. Install with [npm](http://github.com/isaacs/npm):
  5. npm install mime
  6. ## API - Queries
  7. ### mime.lookup(path)
  8. 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.
  9. var mime = require('mime');
  10. mime.lookup('/path/to/file.txt'); // => 'text/plain'
  11. mime.lookup('file.txt'); // => 'text/plain'
  12. mime.lookup('.TXT'); // => 'text/plain'
  13. mime.lookup('htm'); // => 'text/html'
  14. ### mime.default_type
  15. Sets the mime type returned when `mime.lookup` fails to find the extension searched for. (Default is `application/octet-stream`.)
  16. ### mime.extension(type)
  17. Get the default extension for `type`
  18. mime.extension('text/html'); // => 'html'
  19. mime.extension('application/octet-stream'); // => 'bin'
  20. ### mime.charsets.lookup()
  21. Map mime-type to charset
  22. mime.charsets.lookup('text/plain'); // => 'UTF-8'
  23. (The logic for charset lookups is pretty rudimentary. Feel free to suggest improvements.)
  24. ## API - Defining Custom Types
  25. The following APIs allow you to add your own type mappings within your project. If you feel a type should be included as part of node-mime, see [requesting new types](https://github.com/broofa/node-mime/wiki/Requesting-New-Types).
  26. ### mime.define()
  27. Add custom mime/extension mappings
  28. mime.define({
  29. 'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'],
  30. 'application/x-my-type': ['x-mt', 'x-mtt'],
  31. // etc ...
  32. });
  33. mime.lookup('x-sft'); // => 'text/x-some-format'
  34. The first entry in the extensions array is returned by `mime.extension()`. E.g.
  35. mime.extension('text/x-some-format'); // => 'x-sf'
  36. ### mime.load(filepath)
  37. Load mappings from an Apache ".types" format file
  38. mime.load('./my_project.types');
  39. The .types file format is simple - See the `types` dir for examples.