租房小程序前端代码
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.

186 lines
5.5 KiB

3 months ago
  1. <!--
  2. -- This file is auto-generated from src/README_js.md. Changes should be made there.
  3. -->
  4. # Mime
  5. A comprehensive, compact MIME type module.
  6. [![Build Status](https://travis-ci.org/broofa/mime.svg?branch=master)](https://travis-ci.org/broofa/mime)
  7. ## Version 2 Notes
  8. Version 2 is a breaking change from 1.x as the semver implies. Specifically:
  9. * `lookup()` renamed to `getType()`
  10. * `extension()` renamed to `getExtension()`
  11. * `charset()` and `load()` methods have been removed
  12. If you prefer the legacy version of this module please `npm install mime@^1`. Version 1 docs may be found [here](https://github.com/broofa/mime/tree/v1.4.0).
  13. ## Install
  14. ### NPM
  15. ```
  16. npm install mime
  17. ```
  18. ### Browser
  19. It is recommended that you use a bundler such as
  20. [webpack](https://webpack.github.io/) or [browserify](http://browserify.org/) to
  21. package your code. However, browser-ready versions are available via wzrd.in.
  22. E.g. For the full version:
  23. <script src="https://wzrd.in/standalone/mime@latest"></script>
  24. <script>
  25. mime.getType(...); // etc.
  26. </script>
  27. Or, for the `mime/lite` version:
  28. <script src="https://wzrd.in/standalone/mime%2flite@latest"></script>
  29. <script>
  30. mimelite.getType(...); // (Note `mimelite` here)
  31. </script>
  32. ## Quick Start
  33. For the full version (800+ MIME types, 1,000+ extensions):
  34. ```javascript
  35. const mime = require('mime');
  36. mime.getType('txt'); // ⇨ 'text/plain'
  37. mime.getExtension('text/plain'); // ⇨ 'txt'
  38. ```
  39. See [Mime API](#mime-api) below for API details.
  40. ## Lite Version
  41. There is also a "lite" version of this module that omits vendor-specific
  42. (`*/vnd.*`) and experimental (`*/x-*`) types. It weighs in at ~2.5KB, compared
  43. to 8KB for the full version. To load the lite version:
  44. ```javascript
  45. const mime = require('mime/lite');
  46. ```
  47. ## Mime .vs. mime-types .vs. mime-db modules
  48. For those of you wondering about the difference between these [popular] NPM modules,
  49. here's a brief rundown ...
  50. [`mime-db`](https://github.com/jshttp/mime-db) is "the source of
  51. truth" for MIME type information. It is not an API. Rather, it is a canonical
  52. dataset of mime type definitions pulled from IANA, Apache, NGINX, and custom mappings
  53. submitted by the Node.js community.
  54. [`mime-types`](https://github.com/jshttp/mime-types) is a thin
  55. wrapper around mime-db that provides an API drop-in compatible(ish) with `mime @ < v1.3.6` API.
  56. `mime` is, as of v2, a self-contained module bundled with a pre-optimized version
  57. of the `mime-db` dataset. It provides a simplified API with the following characteristics:
  58. * Intelligently resolved type conflicts (See [mime-score](https://github.com/broofa/mime-score) for details)
  59. * Method naming consistent with industry best-practices
  60. * Compact footprint. E.g. The minified+compressed sizes of the various modules:
  61. Module | Size
  62. --- | ---
  63. `mime-db` | 18 KB
  64. `mime-types` | same as mime-db
  65. `mime` | 8 KB
  66. `mime/lite` | 2 KB
  67. ## Mime API
  68. Both `require('mime')` and `require('mime/lite')` return instances of the MIME
  69. class, documented below.
  70. Note: Inputs to this API are case-insensitive. Outputs (returned values) will
  71. be lowercase.
  72. ### new Mime(typeMap, ... more maps)
  73. Most users of this module will not need to create Mime instances directly.
  74. However if you would like to create custom mappings, you may do so as follows
  75. ...
  76. ```javascript
  77. // Require Mime class
  78. const Mime = require('mime/Mime');
  79. // Define mime type -> extensions map
  80. const typeMap = {
  81. 'text/abc': ['abc', 'alpha', 'bet'],
  82. 'text/def': ['leppard']
  83. };
  84. // Create and use Mime instance
  85. const myMime = new Mime(typeMap);
  86. myMime.getType('abc'); // ⇨ 'text/abc'
  87. myMime.getExtension('text/def'); // ⇨ 'leppard'
  88. ```
  89. If more than one map argument is provided, each map is `define()`ed (see below), in order.
  90. ### mime.getType(pathOrExtension)
  91. Get mime type for the given path or extension. E.g.
  92. ```javascript
  93. mime.getType('js'); // ⇨ 'application/javascript'
  94. mime.getType('json'); // ⇨ 'application/json'
  95. mime.getType('txt'); // ⇨ 'text/plain'
  96. mime.getType('dir/text.txt'); // ⇨ 'text/plain'
  97. mime.getType('dir\\text.txt'); // ⇨ 'text/plain'
  98. mime.getType('.text.txt'); // ⇨ 'text/plain'
  99. mime.getType('.txt'); // ⇨ 'text/plain'
  100. ```
  101. `null` is returned in cases where an extension is not detected or recognized
  102. ```javascript
  103. mime.getType('foo/txt'); // ⇨ null
  104. mime.getType('bogus_type'); // ⇨ null
  105. ```
  106. ### mime.getExtension(type)
  107. Get extension for the given mime type. Charset options (often included in
  108. Content-Type headers) are ignored.
  109. ```javascript
  110. mime.getExtension('text/plain'); // ⇨ 'txt'
  111. mime.getExtension('application/json'); // ⇨ 'json'
  112. mime.getExtension('text/html; charset=utf8'); // ⇨ 'html'
  113. ```
  114. ### mime.define(typeMap[, force = false])
  115. Define [more] type mappings.
  116. `typeMap` is a map of type -> extensions, as documented in `new Mime`, above.
  117. By default this method will throw an error if you try to map a type to an
  118. extension that is already assigned to another type. Passing `true` for the
  119. `force` argument will suppress this behavior (overriding any previous mapping).
  120. ```javascript
  121. mime.define({'text/x-abc': ['abc', 'abcd']});
  122. mime.getType('abcd'); // ⇨ 'text/x-abc'
  123. mime.getExtension('text/x-abc') // ⇨ 'abc'
  124. ```
  125. ## Command Line
  126. mime [path_or_extension]
  127. E.g.
  128. > mime scripts/jquery.js
  129. application/javascript
  130. ----
  131. Markdown generated from [src/README_js.md](src/README_js.md) by [![RunMD Logo](http://i.imgur.com/h0FVyzU.png)](https://github.com/broofa/runmd)