What is content-type?
The content-type npm package is used to create and parse HTTP Content-Type header according to RFC 7231. It allows you to parse content-type strings to objects and format content-type objects to strings.
What are content-type's main functionalities?
Parsing Content-Type
This feature allows you to parse a Content-Type string, returning an object with the media type and parameters like charset.
const contentType = require('content-type');
const parsed = contentType.parse('text/html; charset=utf-8');
Formatting Content-Type
This feature enables you to format an object representing a Content-Type into a string that can be used in HTTP headers.
const contentType = require('content-type');
const format = contentType.format({ type: 'text/html', parameters: { charset: 'utf-8' } });
Other packages similar to content-type
mime-types
The mime-types package is similar to content-type as it provides the ability to look up the content-type based on a file extension and vice versa. It is more focused on the association between file extensions and mime types rather than parsing and formatting Content-Type headers.
mime
The mime package is another alternative that allows you to map between MIME types and file extensions. It provides functionality to define custom mime type mappings and is more comprehensive in terms of the number of file types it supports compared to content-type.
negotiator
Negotiator is an HTTP content negotiation library that goes beyond just handling Content-Type headers. It can be used to negotiate language, charset, encoding, etc., based on the Accept-* headers from the client. It is more complex and offers broader functionality for handling HTTP negotiations.
content-type
Create and parse HTTP Content-Type header according to RFC 7231
Installation
$ npm install content-type
API
var contentType = require('content-type')
contentType.parse(string)
var obj = contentType.parse('image/svg+xml; charset=utf-8')
Parse a Content-Type
header. This will return an object with the following
properties (examples are shown for the string 'image/svg+xml; charset=utf-8'
):
-
type
: The media type (the type and subtype, always lower case).
Example: 'image/svg+xml'
-
parameters
: An object of the parameters in the media type (name of parameter
always lower case). Example: {charset: 'utf-8'}
Throws a TypeError
if the string is missing or invalid.
contentType.parse(req)
var obj = contentType.parse(req)
Parse the Content-Type
header from the given req
. Short-cut for
contentType.parse(req.headers['content-type'])
.
Throws a TypeError
if the Content-Type
header is missing or invalid.
contentType.parse(res)
var obj = contentType.parse(res)
Parse the Content-Type
header set on the given res
. Short-cut for
contentType.parse(res.getHeader('content-type'))
.
Throws a TypeError
if the Content-Type
header is missing or invalid.
contentType.format(obj)
var str = contentType.format({
type: 'image/svg+xml',
parameters: { charset: 'utf-8' }
})
Format an object into a Content-Type
header. This will return a string of the
content type for the given object with the following properties (examples are
shown that produce the string 'image/svg+xml; charset=utf-8'
):
Throws a TypeError
if the object contains an invalid type or parameter names.
License
MIT