@ 浏览/ 类型- is
浏览器兼容"type-is"!
此模块捆绑 类型是 浏览器模块 !
类型是
推断请求的内容类型.
安装
这是一个 节点.js 模块
npm 登记。安装使用
npm install 命令数字 :
$ npm install type-is
API 密码
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')
})
类型( 请求, 类型)
检查是否 request 这是其中之一 types如果请求没有机构,
即使有这样的一个 Content-Type 标题,然后 null 已返回。 如果
Content-Type 信头无效或不匹配任何 types,则
false 已返回。 否则,将返回匹配类型的字符串.
这个 request 参数是 Node.js HTTP 请求。 这个 types
参数是一个类型字符串的数组.
每一类型 types 数组可以是以下之一:
- 文件扩展名, 如
json。如果匹配,此名称将返回. - 类似 MIME 类型的
application/json。 。 。 . - 带有通配符的 MIME 类型
*/*或者说*/json或者说application/*。 。 。 . 如果匹配, 完整的 MIME 类型将返回 . - 这样的后缀
+json。这可以结合一个通配符,例如*/vnd+json或者说application/*+json。完整 MIME 类型将返回 如果匹配的话.
说明投入和返回值的一些例子:
// 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.has Body( 请求)
如果给定的返回布尔 request 拥有一具尸体,无论
Content-Type 头曰.
拥有一具尸体与身体有多大(可能是0字节)没有关系. 这类似于文件存在的工作原理. 如果真的存在,那么这个 表示有数据可以从 Node.js 请求流读取.
if (typeis.hasBody(req)) {
// read the body, since there is one
req.on('data', function (chunk) {
// ...
})
}
type.is( 中间类型, 类型)
检查是否 mediaType 这是其中之一 types如果 mediaType 无效
或不匹配任何一个 types,则 false 已返回。 否则,a
返回匹配类型的字符串.
这个 mediaType 参数是:
媒体类型 绳子。 这个 types 参数
是类型字符串的数组.
每一类型 types 数组可以是以下之一:
- 文件扩展名, 如
json。如果匹配,此名称将返回. - 类似 MIME 类型的
application/json。 。 。 . - 带有通配符的 MIME 类型
*/*或者说*/json或者说application/*。 。 。 . 如果匹配, 完整的 MIME 类型将返回 . - 这样的后缀
+json。这可以结合一个通配符,例如*/vnd+json或者说application/*+json。完整 MIME 类型将返回 如果匹配的话.
说明投入和返回值的一些例子:
var mediaType = '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,记入通配符.
通配符只能存在于介质类型中的子类型部分,并且只能使用
输入 expected 价值(截至 actual 应该是真正的媒体类型。 A 类
后缀仍然可以包含,即使带有通配符子类型。 如果输入是
畸形, false 将返回.
typeis.match('text/html', 'text/html') // => true
typeis.match('*/html', 'text/html') // => true
typeis.match('text/*', 'text/html') // => true
typeis.match('*/*', 'text/html') // => true
typeis.match('*/*+json', 'application/x-custom+json') // => true
类型. 正常化( 类型)
规范 a type 绳子。 这项工作通过以下方式进行:
- 如果
type不是绳子,false已返回. - 如果字符串开始
+(所以这是一个+suffix单手像+json, (中文(简体) ), 然后扩充,以包含完整的通配符符号*/*+suffix。 。 。 . - 如果字符串包含一个
/,然后返回为类型. - 否则字符串被假定为文件扩展名,映射出媒体类型是
返回,或
false没有地图.
其中包括两个特殊的绘图:
'multipart'- > (中文)'multipart/*''urlencoded'- > (中文)'application/x-www-form-urlencoded'
实例
实例正文解析器
var express = require('express')
var typeis = require('type-is')
var app = express()
app.use(function bodyParser (req, res, next) {
if (!typeis.hasBody(req)) {
return next()
}
switch (typeis(req, ['urlencoded', 'json', 'multipart'])) {
case 'urlencoded':
// parse urlencoded body
throw new Error('implement urlencoded body parsing')
case 'json':
// parse json body
throw new Error('implement json body parsing')
case 'multipart':
// parse multipart body
throw new Error('implement multipart body parsing')
default:
// 415 error code
res.statusCode = 415
res.end()
break
}
})