Skip to content

Instantly share code, notes, and snippets.

@jxson jxson/decorate.js

Created May 21, 2013
Embed
What would you like to do?
Example req helpers for content negotiation.
req.is = function(type){
// http://www.w3.org/Protocols/rfc2616/rfc2616.txt section 7.2.1
var ct = req.headers['content-type'] || 'application/octet-stream'
, mime = require('mime')
, index = ct.indexOf(';')
if (index > -1) ct = ct.substring(0, index)
mime.define({
'application/x-www-form-urlencoded': [ 'form' ]
})
return mime.lookup(type) === ct
}
req.wants = function(type){
var Negotiator = require('negotiator')
, mime = require('mime')
, negotiator = new Negotiator(req)
, types = negotiator.preferredMediaTypes() || []
return types[0] === mime.lookup(type)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.