mimetype-js
Overview
I find keep making these file extension lookup tables for mime types. It's
about time I put it in a module to save me the trouble.
This version differs from rsdoiel's version because we are not looking for the path module at all. This means that we can use this for React Native (which has Node, but doesn't have Path).
Examples
general case
var mimetype = require('mimetype');
console.log(mimetype.lookup("myfile.txt"));
mimetype.set('.exotic', 'x-application/experimental');
console.log(mimetype.lookup("myfile.exotic"));
mimetype.del('.exotic');
console.log(mimetype.lookup("myfile.exitoc"));
mimetype.forEach(function (ext, mime_type_string) {
console.log(ext, mime_type_string);
});
Special cases
Sometimes detecting by filename extensions isn't work and you want to
default to a general purposes mime-type (e.g. text/plain, application/octet-stream).
var mimetype = require('mimetype');
console.log(mimetype.lookup("filename.unknownMimeType");
console.log(mimetype.lookup("filename.unknownMimeType", false, "text/plain");
console.log(mimetype.lookup("filename.unknownMimeType", "UTF-8", "text/plain");
Using mimetype.js with MongoDB Shell
While this was implemented as a NodeJS module it also works under MongoDB's shell.
Instead of including with a "require" you would load the JavaScript file load-mimetype.js.
load("./extras/load-mimetype.js");
print("Check the mime type of test.txt:" + MimeType.lookup("test.txt"));
This would display something like-
MongoDB shell version: 2.2.0
connecting to: test
> load("./extras/load-mimetype.js");
> print("Check the mime type of test.txt: " + MimeType.lookup("test.txt"));
Check the mime type of test.txt: text/plain
>