How to use the lookup function from mime
Find comprehensive JavaScript mime.lookup code examples handpicked from public code repositorys.
mime.lookup is a function in the mime library that returns the MIME type for a given file extension.
GitHub: tj/nshell
187 188 189 190 191 192 193 194 195 196
// auto-editor (silly implementation) shell.on('command', function(e){ var line = e.line.trim(); var type = mime.lookup(line); switch (type) { case 'text/plain': case 'text/css': case 'application/javascript':
170 171 172 173 174 175 176 177 178 179
file.filename = sanitize(node.name); } } // if mimetype is still empty, try to get from the filename if (_.isEmpty(file.mimeType) && !_.isEmpty(file.filename)) { file.mimeType = mime.lookup(file.filename); } // if extension is still empty, try to get from filename if (_.isEmpty(file.extension) && !_.isEmpty(file.filename)) { file.extension = Path.extname(file.filename);
How does mime.lookup work?
mime.lookup is a function in the mime library that is used to determine the MIME type for a given file extension. When called with a file extension as its argument, mime.lookup returns the corresponding MIME type for that extension. If the extension is not recognized by the library, the function returns a default MIME type of application/octet-stream. Internally, mime.lookup maintains a mapping of file extensions to MIME types in a lookup table. The table is loaded from a file called types.json when the library is initialized. The types.json file contains a list of MIME type definitions for common file extensions. If the MIME type for a file extension is not found in the lookup table, mime.lookup uses a set of heuristics to try to determine the correct MIME type based on the file's contents. These heuristics include examining the file's contents for known patterns or magic numbers that indicate its file type. Note that mime.lookup is part of the mime library, which is a collection of utility functions for working with MIME types in JavaScript. It is useful for tasks such as serving static files over HTTP, validating file uploads, or generating file download links.
824 825 826 827 828 829 830 831 832 833 834 835 836
SendStream.prototype.type = function type (path) { var res = this.res if (res.getHeader('Content-Type')) return var type = mime.lookup(path) if (!type) { debug('no content-type') return
410 411 412 413 414 415 416 417 418 419
} // serve the file if (stats.isFile()) { var contentType = mime.lookup(absPath); var headers; if (contentType) { headers = {
Ai Example
1 2 3 4 5 6 7
const mime = require("mime"); const fileExtension = "jpg"; const mimeType = mime.lookup(fileExtension); console.log(mimeType); // Output: image/jpeg
In this example, we start by importing the mime library. We then define a variable called fileExtension that contains the file extension we want to look up the MIME type for. In this case, we're using 'jpg' as an example. We call mime.lookup with fileExtension as its argument to get the corresponding MIME type. We store the result in a variable called mimeType. Finally, we log mimeType to the console, which outputs 'image/jpeg' because that is the MIME type for files with a .jpg extension. Note that this is just a simple example, and mime.lookup can be used with any file extension to get its corresponding MIME type.
mime.getType is the most popular function in mime (35 examples)