Awesome 社區模塊

Mime

A comprehensive, compact MIME type module.

Build Status

Version 2 Notes

Version 2 is a breaking change from 1.x as the semver implies. Specifically:

  • lookup() renamed to getType()
  • extension() renamed to getExtension()
  • charset()andload()

If you prefer the legacy version of this module pleasenpm install mime@^1

Install

NPM

1
npm install mime

Browser

It is recommended that you use a bundler such as

1 2 3 4
<script src="https://wzrd.in/standalone/mime@latest"></script> <script> mime.getType(...); // etc. <script>

Or, for themime/lite

1 2 3 4
<script src="https://wzrd.in/standalone/mime%2flite@latest"></script> <script> mimelite.getType(...); // (Note `mimelite` here) <script>

Quick Start

For the full version (800+ MIME types, 1,000+ extensions):

1 2 3 4 5
const mime = require('mime'); mime.getType('txt'); // ⇨ 'text/plain' mime.getExtension('text/plain'); // ⇨ 'txt'

See

Lite Version

There is also a "lite" version of this module that omits vendor-specific (*/vnd.**/x-*

1
const mime = require('mime/lite');

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-typesis a thin wrapper around mime-db that provides an API drop-in compatible(ish) withmime @ < v1.3.6

mimeis, as of v2, a self-contained module bundled with a pre-optimized version of themime-db

  • Intelligently resolved type conflicts (See
  • Method naming consistent with industry best-practices
  • Compact footprint. Eg The minified+compressed sizes of the various modules:
Module Size
mime-db 18 KB
mime-types same as mime-db
mime 8 KB
mime/lite 2 KB

Mime API

Bothrequire('mime')require('mime/lite')

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 ...

1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Require Mime class const Mime = require('mime/Mime'); // Define mime type -> extensions map const typeMap = { 'text/abc': ['abc', 'alpha', 'bet'], 'text/def': ['leppard'] }; // Create and use Mime instance const myMime = new Mime(typeMap); myMime.getType('abc'); // ⇨ 'text/abc' myMime.getExtension('text/def'); // ⇨ 'leppard'

If more than one map argument is provided, each map isdefine()

mime.getType(pathOrExtension)

Get mime type for the given path or extension. Eg

1 2 3 4 5 6 7 8 9
mime.getType('js'); // ⇨ 'application/javascript' mime.getType('json'); // ⇨ 'application/json' mime.getType('txt'); // ⇨ 'text/plain' mime.getType('dir/text.txt'); // ⇨ 'text/plain' mime.getType('dir\\text.txt'); // ⇨ 'text/plain' mime.getType('.text.txt'); // ⇨ 'text/plain' mime.getType('.txt'); // ⇨ 'text/plain'

null is returned in cases where an extension is not detected or recognized

1 2 3
mime.getType('foo/txt'); // ⇨ null mime.getType('bogus_type'); // ⇨ null

mime.getExtension(type)

Get extension for the given mime type. Charset options (often included in Content-Type headers) are ignored.

1 2 3 4
mime.getExtension('text/plain'); // ⇨ 'txt' mime.getExtension('application/json'); // ⇨ 'json' mime.getExtension('text/html; charset=utf8'); // ⇨ 'html'

mime.define(typeMap[, force = false])

Define [more] type mappings.

typeMapis a map of type -> extensions, as documented innew Mime

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. Passingtrueforce

1 2 3 4 5
mime.define({'text/x-abc': ['abc', 'abcd']}); mime.getType('abcd'); // ⇨ 'text/x-abc' mime.getExtension('text/x-abc') // ⇨ 'abc'

Command Line

1
mime [path_or_extension]

Eg

1 2
> mime scripts/jquery.js application/javascript

Markdown generated from src/README_js.md by RunMD Logo