What is mime?
The mime npm package is a utility for handling and transforming file MIME types. It provides functionality to lookup the MIME type for a file based on its extension, determine the default extension for a MIME type, and define custom MIME type mappings.
What are mime's main functionalities?
Lookup MIME type by extension
This feature allows you to get the MIME type for a given file extension. In the code sample, we are looking up the MIME type for a '.json' file, which returns 'application/json'.
const mime = require('mime');
const mimeType = mime.getType('json'); // 'application/json'
Lookup extension by MIME type
This feature enables you to find the default file extension for a given MIME type. In the code sample, we are finding the extension for the MIME type 'application/json', which returns 'json'.
const mime = require('mime');
const extension = mime.getExtension('application/json'); // 'json'
Define custom MIME type mappings
This feature allows you to define custom MIME type mappings. In the code sample, we are adding a custom MIME type 'text/x-some-format' with multiple extensions. The second argument 'true' indicates that the custom types should take precedence over the built-in types.
const mime = require('mime');
mime.define({ 'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'] }, true);
Other packages similar to mime
mime-types
The 'mime-types' package is similar to 'mime' in that it provides MIME type lookup based on file extension and vice versa. It is based on the mime-db dataset, which is a comprehensive dataset of MIME types compiled from various sources. Compared to 'mime', it might offer a more extensive and updated list of MIME types.
file-type
The 'file-type' package goes beyond simple MIME type lookup by extension; it can detect the actual MIME type of a file or a Buffer by checking the file's magic numbers (file signatures). This can be more reliable than 'mime' when dealing with files that have incorrect or missing extensions.
mime
Comprehensive MIME type mapping API based on mime-db module.
Install
Install with npm:
npm install mime
Contributing / Testing
npm run test
Command Line
mime [path_string]
E.g.
> mime scripts/jquery.js
application/javascript
API - Queries
mime.lookup(path)
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.
var mime = require('mime');
mime.lookup('/path/to/file.txt');
mime.lookup('file.txt');
mime.lookup('.TXT');
mime.lookup('htm');
mime.default_type
Sets the mime type returned when mime.lookup
fails to find the extension searched for. (Default is application/octet-stream
.)
mime.extension(type)
Get the default extension for type
mime.extension('text/html');
mime.extension('application/octet-stream');
mime.charsets.lookup()
Map mime-type to charset
mime.charsets.lookup('text/plain');
(The logic for charset lookups is pretty rudimentary. Feel free to suggest improvements.)
API - Defining Custom Types
Custom type mappings can be added on a per-project basis via the following APIs.
mime.define()
Add custom mime/extension mappings
mime.define({
'text/x-some-format': ['x-sf', 'x-sft', 'x-sfml'],
'application/x-my-type': ['x-mt', 'x-mtt'],
});
mime.lookup('x-sft');
The first entry in the extensions array is returned by mime.extension()
. E.g.
mime.extension('text/x-some-format');
mime.load(filepath)
Load mappings from an Apache ".types" format file
mime.load('./my_project.types');
The .types file format is simple - See the types
dir for examples.