How to use mime-types

Comprehensive mime-types code examples:

How to use mime-types.charsets:

129
130
131
132
133
134
135
136
137
138
if(error) return next(error)
if(!body) return next() // 404

var outputType = terraform.helpers.outputType(sourceFile)
var mimeType   = helpers.mimeType(outputType)
var charset    = mime.charsets.lookup(mimeType)
rsp.statusCode = 200
rsp.setHeader('Content-Type', mimeType + (charset ? '; charset=' + charset : ''))
rsp.setHeader('Content-Length', Buffer.byteLength(body, charset));
rsp.end(body)

How to use mime-types.contentType:

39
40
41
42
43
44
45
46
47
48

// Returns a middleware that injects the SocketIO JavaScript
// and a link to the clients side adapters into any HTML file
module.exports = function (configuration) {
  return injector(function (req, res) {
    var header = res.getHeader('content-type') || mime.contentType(path.extname(url.parse(req.url).pathname));
    if (res._header) {
      var matches = res._header.match(/content-type:[\s*](.*)/i);
      header = matches && matches[1];
    }

How to use mime-types.default:

1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
for (const filePath of file || []) {
  const data = import_fs3.default.readFileSync(filePath).toString(encoding);
  files.push({
    name: filePath,
    encoding,
    type: import_mime_types.default.lookup(filePath),
    data
  });
}
const answersArg = answers ? await this.jsonData(answers) : null;

How to use mime-types.extension:

5
6
7
8
9
10
11
12
13
14
try {
    let output = '';

    if (input && input.length > 0) {                

        const result = mime.extension(input);   

        if (result) {
            output = result;
        }

How to use mime-types.lookup:

175
176
177
178
179
180
181
182
183
184
type(mime_type) {
    // Remove leading dot from mime type if present
    if (mime_type.startsWith('.')) mime_type = mime_type.substring(1);

    // Determine proper mime type and send response
    this.header('content-type', mime_types.lookup(mime_type) || 'text/plain');
    return this;
}

/**