Skip to content
Myr

Merge branch 'is'

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 · parents a2a1aad, 0cb4677 · Browse files · 3 files+154 added−0 removed
This commit

State

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

Diff stats

3 files+154 added−0 removed

  • tests:+79 added−0 removed
  • other:+75 added−0 removed

3 files+154 added−0 removed

modified:lib/express/request.js+59 added
191 unmodified lines
192 return msgs;193 }194};195196/**197 * Check if the incoming request contains the "Content-Type" 198 * header field, and it contains the give mime `type`.199 *200 * Examples:201 *202 * // With Content-Type: text/html; charset=utf-8203 * req.is('html');204 * // => true205 *206 * req.is('text/html');207 * // => true208 *209 * // When Content-Type is application/json210 * req.is('json');211 * // => true212 *213 * req.is('application/json');214 * // => true215 *216 * req.is('html');217 * // => false218 * 219 * Ad-hoc callbacks can also be registered with Express, to perform220 * assertions again the request, for example if we need an expressive221 * way to check if our incoming request is an image, we can register "an image"222 * callback:223 * 224 * app.is('an image', function(req){225 * return 0 == req.headers['content-type'].indexOf('image');226 * });227 * 228 * Now within our route callbacks, we can use to to assert content types229 * such as "image/jpeg", "image/png", etc.230 * 231 * app.post('/image/upload', function(req, res, next){232 * if (req.is('an image')) {233 * // do something234 * } else {235 * next();236 * }237 * });238 * 239 * @param {String} type240 * @return {Boolean}241 * @api public242 */243244http.IncomingMessage.prototype.is = function(type){245 var fn = this.app.is(type);246 if (fn) return fn(this);247 var contentType = this.headers['content-type'];248 if (!contentType) return;249 if (!~type.indexOf('/')) type = mime.type('.' + type);250 // TODO: remove when connect no longer appends charset...251 if (~type.indexOf(';')) type = type.split(';')[0];252 return ~contentType.indexOf(type);253};254255// Callback for isXMLHttpRequest / xhr256257function isxhr() {258 return this.header('X-Requested-With', '').toLowerCase() === 'xmlhttprequest';
11 unmodified lines
modified:lib/express/server.js+16 added
26 unmodified lines
27 var self = this;28 this.config = {};29 this.settings = {};30 this.redirects = {};31 this.isCallbacks = {};32 this.viewHelpers = {};33 this.dynamicViewHelpers = {};34 this.errorHandlers = [];35 connect.Server.call(this, middleware || []);
196 unmodified lines
232 return this;233};234235/**236 * Register the given callback `fn` for the given `type`.237 *238 * @param {String} type239 * @param {Function} fn240 * @return {Server} for chaining241 * @api public242 */243244Server.prototype.is = function(type, fn){245 if (!fn) return this.isCallbacks[type];246 this.isCallbacks[type] = fn;247 return this;248};249250/**251 * Assign `setting` to `val`, or return `setting`'s value.252 * Mounted servers inherit their parent server's settings.253 *254 * @param {String} setting
111 unmodified lines
modified:test/request.test.js+79 added