Skip to content
Myr

Added `res.respondTo(obj)`

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 892b605 · Browse files · 3 files+148 added−6 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+148 added−6 removed

  • tests:+65 added−0 removed
  • other:+83 added−6 removed

3 files+148 added−6 removed

modified:lib/response.js+65 added
12 unmodified lines
13 , http = require('http')14 , path = require('path')15 , connect = require('connect')16 , utils = connect.utils17 , accept = require('./utils').accept18 , statusCodes = http.STATUS_CODES19 , send = connect.static.send20 , mime = require('mime')21 , basename = path.basename
265 unmodified lines
287 return this.set('Content-Type', mime.lookup(type));288};289290/**291 * Respond to the Acceptable formats using an `obj`292 * of mime-type callbacks.293 *294 * This method uses `req.accepted`, an array of295 * acceptable types ordered by their quality values.296 * When "Accept" is not present the _first_ callback297 * is invoked, otherwise the first match is used. When298 * no match is performed the server responds with299 * 406 "Not Acceptable".300 *301 * Examples:302 *303 * res.respondTo({304 * 'text/plain': function(){305 * res.type('text').send('hey');306 * },307 * 308 * 'text/html': function(){309 * res.send('<p>hey</p>');310 * },311 * 312 * 'appliation/json': function(){313 * res.send({ message: 'hey' });314 * }315 * });316 *317 * @param {Object} obj318 * @return {ServerResponse} for chaining319 * @api public320 */321322res.respondTo = function(obj){323 var keys = Object.keys(obj)324 , req = this.req325 , next = req.next326 , accepted = req.accepted327 , type328 , key;329330 out:331 for (var i = 0, len = accepted.length; i < len; ++i) {332 for (var j = 0, jlen = keys.length; j < jlen; ++j) {333 if (accept(keys[j].split('/'), accepted[i])) {334 key = keys[j];335 break out;336 }337 }338 }339340 if (!accepted.length) key = keys.shift();341342 if (key) {343 obj[key](req, res, next);344 } else {345 var err = new Error('Not Acceptable');346 err.status = 406;347 err.types = keys;348 next(err);349 }350351 return this;352};353354/**355 * Set _Content-Disposition_ header to _attachment_ with optional `filename`.356 *357 * @param {String} filename358 * @return {ServerResponse}
268 unmodified lines
modified:lib/utils.js+18 added-6 removed
added:test/res.respondTo.js+65 added
12var express = require('../')3 , request = require('./support/http')4 , utils = require('../lib/utils');56var app = express();78app.use(function(req, res, next){9 res.respondTo({10 'text/plain': function(){11 res.type('text').send('hey');12 },1314 'text/html': function(){15 res.send('<p>hey</p>');16 },17 18 'appliation/json': function(){19 res.send({ message: 'hey' });20 }21 });22});2324app.use(function(err, req, res, next){25 res.send(406, 'Supports: ' + err.types.join(', '));26})2728describe('req', function(){29 describe('.respondTo(obj)', function(){30 it('should utilize qvalues in negotiation', function(done){31 request(app)32 .get('/')33 .set('Accept', 'text/html; q=.5, appliation/json, */*; q=.1')34 .expect('{"message":"hey"}', done);35 })3637 it('should allow wildcard type/subtypes', function(done){38 request(app)39 .get('/')40 .set('Accept', 'text/html; q=.5, appliation/*, */*; q=.1')41 .expect('{"message":"hey"}', done);42 })4344 describe('when Accept is not present', function(){45 it('should invoke the first callback', function(done){46 request(app)47 .get('/')48 .expect('hey', done);49 })50 })5152 describe('when no match is made', function(){53 it('should should respond with 406 not acceptable', function(done){54 request(app)55 .get('/')56 .set('Accept', 'foo/bar')57 .end(function(res){58 res.should.have.status(406);59 res.body.should.equal('Supports: text/plain, text/html, appliation/json');60 done();61 });62 })63 })64 })65})