Skip to content
Myr

Added extname with no leading "." support to res.contentType)

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

For example now you may: res.contentType("png"); or res.contentType(".png"); both are fine now

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 680766f · Browse files · 2 files+14 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

2 files+14 added−0 removed

  • tests:+8 added−0 removed
  • other:+6 added−0 removed

2 files+14 added−0 removed

modified:lib/express/response.js+6 added
193 unmodified lines
194 * var filename = 'path/to/image.png';195 * res.contentType(filename);196 * // res.headers['Content-Type'] is now "image/png"197 *198 * res.contentType('.html');199 * res.contentType('html');200 * res.contentType('json');201 * res.contentType('png');202 *203 * @param {String} type204 * @return {String} the resolved mime type205 * @api public206 */207208http.ServerResponse.prototype.contentType = function(type){209 if (!~type.indexOf('.')) type = '.' + type;210 return this.header('Content-Type', mime.type(type));211};212213/**
162 unmodified lines
modified:test/response.test.js+8 added
96 unmodified lines
97 res.writeHead(200, res.headers);98 res.end('<p>yay</p>');99 });100 101 app.get('/json', function(req, res, next){102 res.contentType('json');103 res.send('{"foo":"bar"}');104 });105 106 assert.response(app,107 { url: '/html' },108 { body: '<p>yay</p>', headers: { 'Content-Type': 'text/html; charset=utf-8' }});109 assert.response(app,110 { url: '/json' },111 { headers: { 'Content-Type': 'application/json' }});112 },113 114 'test #attachment()': function(){115 var app = express.createServer();
298 unmodified lines