- Notifications
You must be signed in to change notification settings - Fork0
Infer the content-type of a request.
License
rg2011/type-is
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Infer the content-type of a request.
This is aNode.js module available through thenpm registry. Installation is done using thenpm install
command:
$ npm install type-is
varhttp=require('http')vartypeis=require('type-is')http.createServer(function(req,res){varistext=typeis(req,['text/*'])res.end('you '+(istext ?'sent' :'did not send')+' me text')})
Checks if therequest
is one of thetypes
. If the request has no body,even if there is aContent-Type
header, thennull
is returned. If theContent-Type
header is invalid or does not matches any of thetypes
, thenfalse
is returned. Otherwise, a string of the type that matched is returned.
Therequest
argument is expected to be a Node.js HTTP request. Thetypes
argument is an array of type strings.
Each type in thetypes
array can be one of the following:
- A file extension name such as
json
. This name will be returned if matched. - A mime type such as
application/json
. - A mime type with a wildcard such as
*/*
or*/json
orapplication/*
.The full mime type will be returned if matched. - A suffix such as
+json
. This can be combined with a wildcard such as*/vnd+json
orapplication/*+json
. The full mime type will be returnedif matched.
Some examples to illustrate the inputs and returned value:
// req.headers.content-type = 'application/json'typeis(req,['json'])// => 'json'typeis(req,['html','json'])// => 'json'typeis(req,['application/*'])// => 'application/json'typeis(req,['application/json'])// => 'application/json'typeis(req,['html'])// => false
Returns a Boolean if the givenrequest
has a body, regardless of theContent-Type
header.
Having a body has no relation to how large the body is (it may be 0 bytes).This is similar to how file existence works. If a body does exist, then thisindicates that there is data to read from the Node.js request stream.
if(typeis.hasBody(req)){// read the body, since there is onereq.on('data',function(chunk){// ...})}
Checks if themediaType
is one of thetypes
. If themediaType
is invalidor does not matches any of thetypes
, thenfalse
is returned. Otherwise, astring of the type that matched is returned.
ThemediaType
argument is expected to be amedia type string. Thetypes
argumentis an array of type strings.
Each type in thetypes
array can be one of the following:
- A file extension name such as
json
. This name will be returned if matched. - A mime type such as
application/json
. - A mime type with a wildcard such as
*/*
or*/json
orapplication/*
.The full mime type will be returned if matched. - A suffix such as
+json
. This can be combined with a wildcard such as*/vnd+json
orapplication/*+json
. The full mime type will be returnedif matched.
Some examples to illustrate the inputs and returned value:
varmediaType='application/json'typeis.is(mediaType,['json'])// => 'json'typeis.is(mediaType,['html','json'])// => 'json'typeis.is(mediaType,['application/*'])// => 'application/json'typeis.is(mediaType,['application/json'])// => 'application/json'typeis.is(mediaType,['html'])// => false
Match the type stringexpected
withactual
, taking in to account wildcards.A wildcard can only be in the type of the subtype part of a media type and onlyin theexpected
value (asactual
should be the real media type to match). Asuffix can still be included even with a wildcard subtype. If an input ismalformed,false
will be returned.
typeis.match('text/html','text/html')// => truetypeis.match('*/html','text/html')// => truetypeis.match('text/*','text/html')// => truetypeis.match('*/*','text/html')// => truetypeis.match('*/*+json','application/x-custom+json')// => true
Normalize atype
string. This works by performing the following:
- If the
type
is not a string,false
is returned. - If the string starts with
+
(so it is a+suffix
shorthand like+json
),then it is expanded to contain the complete wildcard notation of*/*+suffix
. - If the string contains a
/
, then it is returned as the type. - Else the string is assumed to be a file extension and the mapped media type isreturned, or
false
is there is no mapping.
This includes two special mappings:
'multipart'
->'multipart/*'
'urlencoded'
->'application/x-www-form-urlencoded'
varexpress=require('express')vartypeis=require('type-is')varapp=express()app.use(functionbodyParser(req,res,next){if(!typeis.hasBody(req)){returnnext()}switch(typeis(req,['urlencoded','json','multipart'])){case'urlencoded':// parse urlencoded bodythrownewError('implement urlencoded body parsing')case'json':// parse json bodythrownewError('implement json body parsing')case'multipart':// parse multipart bodythrownewError('implement multipart body parsing')default:// 415 error coderes.statusCode=415res.end()break}})
About
Infer the content-type of a request.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- JavaScript100.0%