Giter Site home page Giter Site logo

node-mime's Introduction

Mime

A comprehensive, compact MIME type module.

Version 2 Notes

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

  • ES6 support required (node@>=6)
  • lookup() renamed to getType()
  • extension() renamed to getExtension()
  • charset() and load() 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

npm install mime

Quick Start

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

const mime = require('mime');

mime.getType('txt');                    // ⇨ 'text/plain'
mime.getExtension('text/plain');        // ⇨ 'txt'

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:

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

Browser-ready Versions

To use this module in the browser, you would typlically use webpack or browserify to package your code. However, browser-ready versions are available via wzrd.in. E.g. For the full version:

<script src="http://wzrd.in/standalone/mime@latest"></script>

Or, for the "lite" version:

<script src="http://wzrd.in/standalone/mime%2flite@latest"></script>

Then:

<script>
mime.getType(...); // etc.
</script>

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:

  • Internally consistent type ⇔ extension mapping. I.e. mime.getType(mime.getExtension(type)) == type will always be true
  • Method naming consistent with industry best-practices
  • Compact footprint. E.g. 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

Both require('mime') and require('mime/lite') return instances of the MIME class, documented below.

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

// 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 is define()ed (see below), in order.

mime.getType(pathOrExtension)

Get mime type for the given path or extension. E.g.

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

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.

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.

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

mime.define({'text/x-abc': ['abc', 'abcd']});

mime.getType('abcd');            // ⇨ 'text/x-abc'
mime.getExtension('text/x-abc')  // ⇨ 'abc'

Command Line

mime [path_or_extension]

E.g.

> mime scripts/jquery.js
application/javascript

Markdown generated from src/README_js.md by RunMD Logo

node-mime's People

Contributors

broofa avatar niftylettuce avatar ionicabizau avatar jfhbrook avatar tootallnate avatar cistov avatar matthewkastor avatar jugglinmike avatar alrra avatar ixti avatar shkuznetsov avatar billymoon avatar bmeck avatar chrisveness avatar denis-sokolov avatar edi9999 avatar francois2metz avatar pgilad avatar jacobrask avatar avoidwork avatar joelpurra avatar mattbab avatar shfx avatar apaleslimghost avatar nateinaction avatar zubairov avatar simonratner avatar taylorhutchison avatar wormss avatar zertosh avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.