https://github.com/deepakjois/node-detect-content-type
Determine the Content-Type of the given data using the MIME Sniffing Standard.
https://github.com/deepakjois/node-detect-content-type
content-type mime-types
Last synced: 5 months ago
JSON representation
Determine the Content-Type of the given data using the MIME Sniffing Standard.
- Host: GitHub
- URL: https://github.com/deepakjois/node-detect-content-type
- Owner: deepakjois
- License: mit
- Created: 2018-02-01T04:22:46.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-07T01:33:03.000Z (over 3 years ago)
- Last Synced: 2025-10-05T19:38:48.319Z (9 months ago)
- Topics: content-type, mime-types
- Language: JavaScript
- Homepage:
- Size: 529 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# detect-content-type
Javascript module to determine the Content-Type of the given data, using the algorithm specified in the [MIME Sniffing Standard](https://mimesniff.spec.whatwg.org/).
This module is useful in cases where it is not possible to determine the Content-Type of the data using filename extension, either because file does not have an extension or the filename is not available.
## Installation
```
npm install detect-content-type
```
or
```
yarn add detect-content-type
```
## Import
ES2015 and above:
```javascript
import detectContentType from 'detect-content-type'
```
Using CommonJS:
```javascript
var detectContentType = require('detect-content-type')
```
## Usage
`detectContentType` takes a Buffer, and determines its Content-Type. It considers at most
512 bytes of data. `detectContentType` always returns a valid MIME type. If it cannot determine a
more specific one, it returns `application/octet-stream`.
```javascript
let ct = detectContentType(Buffer.from("")) // returns 'text/html; charset=utf-8'
```
## Credits
The code in this module is ported nearly line-by-line from the [http.DetectContentType][DetectContentType] method in the Go standard library.
[DetectContentType]: https://golang.org/pkg/net/http/#DetectContentType