1. content-type
Create and parse HTTP Content-Type header
content-type
Package: content-type
Created by: jshttp
Last modified: Mon, 26 Feb 2024 19:48:49 GMT
Version: 1.0.5
License: MIT
Downloads: 122,832,497
Repository: https://github.com/jshttp/content-type

Install

npm install content-type
yarn add content-type

content-type

NPM Version
NPM Downloads
Node.js Version
Build Status
Coverage Status

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'):

  • type: The media type (will be lower-cased). Example: 'image/svg+xml'

  • parameters: An object of the parameters in the media type (name of the
    parameter will be lower-cased). Example: {charset: 'utf-8'}

Throws a TypeError if the object contains an invalid type or parameter names.

License

MIT

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code