type-is
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
media-typer | 0.3.0 | 4.09 kB | MIT | prod | |
mime-types | 2.1.35 | 5.46 kB | MIT | prod |
Type-is is a useful Node.js module that can infer the content type of an HTTP request. This can help in processing different types of request payloads more efficiently in your server-side code. It checks if the request is from one of the specified types and returns accordingly. If the request has no body or if the Content-Type header is invalid, the function will return null or false respectively.
Implementation of type-is in your projects is straightforward and efficient. Type-is can be installed using the npm install command in your terminal as follows:
$ npm install type-is
Using type-is in your project is as easy as requiring the module and using one of its functions in an HTTP server setup such as:
var http = require('http')
var typeis = require('type-is')
http.createServer(function (req, res) {
var istext = typeis(req, ['text/*'])
res.end('you ' + (istext ? 'sent' : 'did not send') + ' me text')
})
In addition to recognizing different content types, type-is also provides a handy method hasBody
to check whether a particular request has a body. This can be used to ensure your server responds appropriately only to requests with a valid body.
Here is a code usage example of the hasBody
method:
if (typeis.hasBody(req)) {
req.on('data', function (chunk) {
// ...
})
}
Another useful function provided by type-is is is
method which checks if a specific mediaType
matches any of the specified types
.
Here is a code usage example of the is
method:
var mediaType = 'application/json'
typeis.is(mediaType, ['json']) // => 'json'
The comprehensive documentation for type-is is available in the type-is GitHub repository. It offers multiple examples, complete API references, and explanations on how to use each API and function. This will help you understand not only how to use the module but also the logic behind it and the potential use cases. The GitHub URL for the package, along with its documentation is: type-is GitHub