Skip to content

Instantly share code, notes, and snippets.

@jxson
Created May 21, 2013 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jxson/5622043 to your computer and use it in GitHub Desktop.
Save jxson/5622043 to your computer and use it in GitHub Desktop.
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