diff --git a/index.js b/index.js index a4e5af8..5fa2840 100644 --- a/index.js +++ b/index.js @@ -609,26 +609,32 @@ SendStream.prototype.send = function(path, stat){ SendStream.prototype.sendFile = function sendFile(path) { var i = 0 var self = this + var isDirectory = false debug('stat "%s"', path); fs.stat(path, function onstat(err, stat) { + isDirectory = stat && stat.isDirectory(); if (err && err.code === 'ENOENT' && !extname(path) && path[path.length - 1] !== sep) { - // not found, check extensions + // not found, check extensions first return next(err) } if (err) return self.onStatError(err) - if (stat.isDirectory()) return self.redirect(self.path) + if (isDirectory) { + return next(err) + } self.emit('file', path, stat) self.send(path, stat) }) function next(err) { if (self._extensions.length <= i) { - return err - ? self.onStatError(err) - : self.error(404) + return isDirectory + ? self.redirect(self.path) : + (err + ? self.onStatError(err) + : self.error(404)) } var p = path + '.' + self._extensions[i++] diff --git a/test/fixtures/crunchy.txt b/test/fixtures/crunchy.txt new file mode 100644 index 0000000..09c61e4 --- /dev/null +++ b/test/fixtures/crunchy.txt @@ -0,0 +1 @@ +crunchy \ No newline at end of file diff --git a/test/fixtures/crunchy/oats.txt b/test/fixtures/crunchy/oats.txt new file mode 100644 index 0000000..5eb473e --- /dev/null +++ b/test/fixtures/crunchy/oats.txt @@ -0,0 +1 @@ +I dunno diff --git a/test/send.js b/test/send.js index 9411286..09b9acd 100644 --- a/test/send.js +++ b/test/send.js @@ -812,7 +812,7 @@ describe('send(file, options)', function(){ .expect(404, done) }) - it('should skip directories', function (done) { + it('should skip directories if file with same name is not present', function (done) { var server = createServer({extensions: ['file', 'dir'], root: fixtures}) request(server) @@ -820,6 +820,14 @@ describe('send(file, options)', function(){ .expect(404, done) }) + it('should return file with same name as directory given extension', function (done) { + var server = createServer({extensions: ['txt'], root: fixtures}) + + request(server) + .get('/crunchy') + .expect(200, 'crunchy', done) + }) + it('should not search if file has extension', function (done) { var server = createServer({extensions: 'html', root: fixtures})