mime
Mime
A comprehensive, compact MIME type module.
Version 2 Notes
Version 2 is a breaking change from 1.x as the semver implies. Specifically:
lookup()
renamed togetType()
extension()
renamed togetExtension()
charset()
andload()
methods have been removed
If you prefer the legacy version of this module please npm install mime@^1
. Version 1 docs may be found here.
Install
NPM
Browser
It is recommended that you use a bundler such as webpack or browserify to package your code. However, browser-ready versions are available via wzrd.in. E.g. For the full version:
Or, for the mime/lite
version:
Quick Start
For the full version (800+ MIME types, 1,000+ extensions):
See Mime API below for API details.
Lite Version
There is also a "lite" version of this module that omits vendor-specific (*/vnd.*
) and experimental (*/x-*
) types. It weighs in at ~2.5KB, compared to 8KB for the full version. To load the lite version:
Mime .vs. mime-types .vs. mime-db modules
For those of you wondering about the difference between these [popular] NPM modules, here's a brief rundown ...
mime-db
is "the source of truth" for MIME type information. It is not an API. Rather, it is a canonical dataset of mime type definitions pulled from IANA, Apache, NGINX, and custom mappings submitted by the Node.js community.
mime-types
is a thin wrapper around mime-db that provides an API drop-in compatible(ish) with mime @ < v1.3.6
API.
mime
is, as of v2, a self-contained module bundled with a pre-optimized version of the mime-db
dataset. It provides a simplified API with the following characteristics:
Intelligently resolved type conflicts (See mime-score for details)
Method naming consistent with industry best-practices
Compact footprint. E.g. The minified+compressed sizes of the various modules:
Module | Size |
---|---|
| 18 KB |
| same as mime-db |
| 8 KB |
| 2 KB |
Mime API
Both require('mime')
and require('mime/lite')
return instances of the MIME class, documented below.
Note: Inputs to this API are case-insensitive. Outputs (returned values) will be lowercase.
new Mime(typeMap, ... more maps)
Most users of this module will not need to create Mime instances directly. However if you would like to create custom mappings, you may do so as follows ...
If more than one map argument is provided, each map is define()
ed (see below), in order.
mime.getType(pathOrExtension)
Get mime type for the given path or extension. E.g.
null
is returned in cases where an extension is not detected or recognized
mime.getExtension(type)
Get extension for the given mime type. Charset options (often included in Content-Type headers) are ignored.
mime.define(typeMap[, force = false])
Define [more] type mappings.
typeMap
is a map of type -> extensions, as documented in new Mime
, above.
By default this method will throw an error if you try to map a type to an extension that is already assigned to another type. Passing true
for the force
argument will suppress this behavior (overriding any previous mapping).
Command Line
E.g.
Markdown generated from src/README_js.md by
Last updated