MIME Format Lookup

2.0.2 · active · verified Wed Apr 22

The `mime-format` library provides a utility for disambiguating the base format of HTTP response bodies based on their `Content-Type` header. It aims to resolve ambiguities, especially between `text/*` and `application/*` types, by maintaining an internal database of textual content types served under `application/*` (e.g., `application/json` is classified as `text`). The current stable version is 2.0.2, with releases appearing to be on an as-needed basis, indicated by the significant gap between v0.2.0 and v2.0.0. Key differentiators include its explicit handling of `application/*` types that are textual in nature, and its ability to guess or mark content types as unknown if they are not in its database, providing granular control over content interpretation.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `mime-format` to identify the base type, specific format, and charset from various `Content-Type` headers, including known types and those requiring guessing.

const mimeFormat = require('mime-format');

// Look up a common XML content type with charset
const xmlResult = mimeFormat.lookup('application/xml; charset=gBk');
console.log('XML result:', xmlResult);
/*
Output:
{
  "type": "text",
  "format": "xml",
  "charset": "gBk"
}
*/

// Example for a JSON content type, which it classifies as 'text'
const jsonResult = mimeFormat.lookup('application/json');
console.log('JSON result:', jsonResult);
/*
Output:
{
  "type": "text",
  "format": "json"
}
*/

// Example for an unknown content type, demonstrating 'guessed' or 'unknown'
const unknownResult = mimeFormat.lookup('application/x-custom-format');
console.log('Unknown result:', unknownResult);
/*
Output (may vary slightly based on internal logic):
{
  "type": "application",
  "format": "raw",
  "guessed": true
}
*/

view raw JSON →