Skip to content
Myr

Fixed res.sendfile() error handling, defer via next()

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

No need to be so specific here.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 25909c0 · Browse files · 2 files+12 added−7 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+12 added−7 removed

  • tests:+6 added−3 removed
  • other:+6 added−4 removed

2 files+12 added−7 removed

modified:lib/express/response.js+6 added-4 removed
88 unmodified lines
89};9091/**92 * Transfer the file at the given `path`. Automatically sets the _Content-Type_93 * response header, and responds with 404 / 500 appropriately.93 * response header via `res.contentType()`.94 *95 * @param {String} path96 * @param {Function} fn97 * @api public
2 unmodified lines
100http.ServerResponse.prototype.sendfile = function(path, fn){101 var self = this;102 fs.readFile(path, function(err, buf){103 if (err) {104 self.send(err.errno === process.ENOENT105 ? 404106 : 500);104 if (fn) {105 fn(err, path);106 } else {107 self.req.next(err);108 }109 } else {110 self.contentType(path);111 self.send(buf);112 fn && fn(null, path);
127 unmodified lines
modified:test/response.test.js+6 added-3 removed
171 unmodified lines
172 },173 174 'test #sendfile()': function(assert){175 var app = express.createServer();176 177 app.get('/*', function(req, res){176177 app.get('/*', function(req, res, next){178 var file = req.params[0],179 filePath = __dirname + '/fixtures/' + file;180 res.sendfile(filePath, function(err, path){181 assert.equal(path, filePath);182 if (err) next(err);183 });184 });185 186 app.use(express.errorHandler());187 188 assert.response(app,189 { url: '/user.json' },190 { body: '{"name":"tj"}', status: 200, headers: { 'Content-Type': 'application/json' }});191 assert.response(app,192 { url: '/hello.haml' },193 { body: '%p Hello World', status: 200, headers: { 'Content-Type': 'application/octet-stream' }});194 assert.response(app,195 { url: '/doesNotExist' },193 { body: 'Not Found', status: 404 });196 { body: 'Internal Server Error', status: 500 });197 assert.response(app,198 { url: '/partials' },199 { body: 'Internal Server Error', status: 500 });200 },
27 unmodified lines