Skip to content

Express request.is

TIP

根据 "Content-Type" 和 type 判断,返回匹配的内容类型

Express
req.is(type)

如果传入请求的 "Content-Type" HTTP 标头字段与 type 参数指定的 MIME 类型匹配,则返回匹配的内容类型。如果请求没有正文,则返回 null。否则返回 false。

Express
r// With Content-Type: text/html; charset=utf-8
req.is('html') // => 'html'
req.is('text/html') // => 'text/html'
req.is('text/*') // => 'text/*'

// When Content-Type is application/json
req.is('json') // => 'json'
req.is('application/json') // => 'application/json'
req.is('application/*') // => 'application/*'

req.is('html')
// => false