Skip to content
Myr

Refactored `req.accepts()`

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 7440c7d · Browse files · 2 files+13 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+13 added−17 removed

  • other:+13 added−17 removed

2 files+13 added−17 removed

modified:lib/request.js+13 added-16 removed
120 unmodified lines
121 * @api public122 */123124req.accepts = function(type){125 var accept = this.header('Accept');125 var accept = this.header('Accept')126 , type = String(type);127128 // when not present or "*/*" accept anything129 if (!accept || '*/*' == accept) return true;130131 // normalize extensions ".json" -> "json"128 if (type && '.' == type[0]) type = type.substr(1);132 if ('.' == type[0]) type = type.substr(1);133130 // when Accept does not exist, or is '*/*' return true 131 if (!accept || '*/*' == accept) {132 return true;133 } else if (type) {134 // allow "html" vs "text/html" etc135 if (!~type.indexOf('/')) type = mime.lookup(type);134 // allow "html" vs "text/html" etc135 if (!~type.indexOf('/')) type = mime.lookup(type);136137 // check if we have a direct match138 if (~accept.indexOf(type)) return true;137 // check if we have a direct match138 if (~accept.indexOf(type)) return true;139140 // check if we have type/*141 type = type.split('/')[0] + '/*';142 return !! ~accept.indexOf(type);143 } else {144 return false;145 }140 // check if we have type/*141 type = type.split('/')[0] + '/*';142 return !! ~accept.indexOf(type);143};144145/**146 * Return the value of param `name` when present or `defaultValue`.
186 unmodified lines
modified:lib/response.js-1 removed
388 unmodified lines
389 var host = req.headers.host390 , tls = req.connection.encrypted;391 url = 'http' + (tls ? 's' : '') + '://' + host + url;392 }393 393394 // Support text/{plain,html} by default395 if (req.accepts('html')) {396 body = '<p>' + statusCodes[status] + '. Redirecting to <a href="' + url + '">' + url + '</a></p>';
11 unmodified lines