diff --git a/lib/ecstatic.js b/lib/ecstatic.js index a00dae7..e7c1424 100755 --- a/lib/ecstatic.js +++ b/lib/ecstatic.js @@ -124,13 +124,26 @@ var ecstatic = module.exports = function (dir, options) { url: parsed.pathname + '.' + defaultExt + ((parsed.search)? parsed.search:'') }, res, next); } - else { - // Try to serve default ./404.html + else if (!handleError) { + // If we're not handling errors/fallbacks, bail out now + middleware({ + url: req.url, + statusCode: 404 + }, res, next); + } + else if (path.basename(parsed.pathname, defaultExt) === '200.') { + // Already tried ./200.html, now try ./404.html middleware({ - url: (handleError ? ('/' + path.join(baseDir, '404.' + defaultExt)) : req.url), + url: '/' + path.join(baseDir, '404.' + defaultExt), statusCode: 404 }, res, next); } + else { + // Try ./200.html before falling back to ./404.html + middleware({ + url: '/' + path.join(baseDir, '200.' + defaultExt), + }, res, next); + } } else if (err) { status[500](res, next, { error: err }); diff --git a/test/fallback-200.js b/test/fallback-200.js new file mode 100644 index 0000000..bcb8e84 --- /dev/null +++ b/test/fallback-200.js @@ -0,0 +1,29 @@ +var test = require('tap').test, + ecstatic = require('../'), + http = require('http'), + request = require('request'), + eol = require('eol'); + +function runTest(path, expectedBody) { + return function (t) { + t.plan(3); + var server = http.createServer(ecstatic(__dirname + '/public/containsFallback')); + + server.listen(0, function () { + var port = server.address().port; + request.get('http://localhost:' + port + path, function (err, res, body) { + t.ifError(err); + t.equal(res.statusCode, 200); + t.equal(eol.lf(body), expectedBody); + server.close(function() { t.end(); }); + }); + }); + }; +} + +test('fallback showsIndex', runTest('/', 'index!!!\n')); +test('fallback showsNonIndex', runTest('/pageTwo.html', 'pageTwo!!!\n')); +test('fallback showsSubDir', runTest('/subdir', 'subdir index!!!\n')); +test('fallback fallsBack 1', runTest('/something', '200.html fallback!!!\n')); +test('fallback fallsBack 2', runTest('/else.html', '200.html fallback!!!\n')); +test('fallback fallsBack 3', runTest('/with/multiple/paths', '200.html fallback!!!\n')); diff --git a/test/public/containsFallback/200.html b/test/public/containsFallback/200.html new file mode 100644 index 0000000..121ee85 --- /dev/null +++ b/test/public/containsFallback/200.html @@ -0,0 +1 @@ +200.html fallback!!! diff --git a/test/public/containsFallback/index.html b/test/public/containsFallback/index.html new file mode 100644 index 0000000..8c9da0b --- /dev/null +++ b/test/public/containsFallback/index.html @@ -0,0 +1 @@ +index!!! diff --git a/test/public/containsFallback/pageTwo.html b/test/public/containsFallback/pageTwo.html new file mode 100644 index 0000000..a142ac7 --- /dev/null +++ b/test/public/containsFallback/pageTwo.html @@ -0,0 +1 @@ +pageTwo!!! diff --git a/test/public/containsFallback/subdir/index.html b/test/public/containsFallback/subdir/index.html new file mode 100644 index 0000000..38b0563 --- /dev/null +++ b/test/public/containsFallback/subdir/index.html @@ -0,0 +1 @@ +subdir index!!!