Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
forked fromjshttp/type-is

Infer the content-type of a request.

License

NotificationsYou must be signed in to change notification settings

rg2011/type-is

 
 

Repository files navigation

NPM VersionNPM DownloadsNode.js VersionBuild StatusTest Coverage

Infer the content-type of a request.

Install

This is aNode.js module available through thenpm registry. Installation is done using thenpm install command:

$ npm install type-is

API

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

typeis(request, types)

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. Thetypesargument is an array of type strings.

Each type in thetypes array can be one of the following:

  • A file extension name such asjson. This name will be returned if matched.
  • A mime type such asapplication/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

typeis.hasBody(request)

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){// ...})}

typeis.is(mediaType, types)

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 asjson. This name will be returned if matched.
  • A mime type such asapplication/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

typeis.match(expected, actual)

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

typeis.normalize(type)

Normalize atype string. This works by performing the following:

  • If thetype 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, orfalse is there is no mapping.

This includes two special mappings:

  • 'multipart' ->'multipart/*'
  • 'urlencoded' ->'application/x-www-form-urlencoded'

Examples

Example body parser

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}})

License

MIT

About

Infer the content-type of a request.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript100.0%

[8]ページ先頭

©2009-2025 Movatter.jp