Created
May 21, 2013 18:21
-
-
Save jxson/5622043 to your computer and use it in GitHub Desktop.
Example req helpers for content negotiation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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