From 3698def8da4faf42a417e8c400ca4d32664b2388 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 6 Sep 2016 14:58:11 +1000 Subject: [PATCH 1/3] Use 200.html as a fallback before 404.html --- lib/ecstatic.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/ecstatic.js b/lib/ecstatic.js index a00dae7..b882a19 100755 --- a/lib/ecstatic.js +++ b/lib/ecstatic.js @@ -124,13 +124,19 @@ 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 (path.basename(parsed.pathname, defaultExt) === '200') { + // Already tried ./200.html, now try ./404.html middleware({ url: (handleError ? ('/' + path.join(baseDir, '404.' + defaultExt)) : req.url), statusCode: 404 }, res, next); } + else { + // Try ./200.html before falling back to ./404.html + middleware({ + url: (handleError ? ('/' + path.join(baseDir, '200.' + defaultExt)) : req.url), + }, res, next); + } } else if (err) { status[500](res, next, { error: err }); From c7be8183e773ff8fce5c1aa1798b30376b99f846 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 6 Sep 2016 15:35:25 +1000 Subject: [PATCH 2/3] need to always try 200 otherwise you get infinite loops --- lib/ecstatic.js | 4 +-- test/fallback-200.js | 29 +++++++++++++++++++ test/public/containsFallback/200.html | 1 + test/public/containsFallback/index.html | 1 + test/public/containsFallback/pageTwo.html | 1 + .../public/containsFallback/subdir/index.html | 1 + 6 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 test/fallback-200.js create mode 100644 test/public/containsFallback/200.html create mode 100644 test/public/containsFallback/index.html create mode 100644 test/public/containsFallback/pageTwo.html create mode 100644 test/public/containsFallback/subdir/index.html diff --git a/lib/ecstatic.js b/lib/ecstatic.js index b882a19..1c07bbe 100755 --- a/lib/ecstatic.js +++ b/lib/ecstatic.js @@ -124,7 +124,7 @@ var ecstatic = module.exports = function (dir, options) { url: parsed.pathname + '.' + defaultExt + ((parsed.search)? parsed.search:'') }, res, next); } - else if (path.basename(parsed.pathname, defaultExt) === '200') { + 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), @@ -134,7 +134,7 @@ var ecstatic = module.exports = function (dir, options) { else { // Try ./200.html before falling back to ./404.html middleware({ - url: (handleError ? ('/' + path.join(baseDir, '200.' + defaultExt)) : req.url), + url: '/' + path.join(baseDir, '200.' + defaultExt), }, res, next); } } 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!!! From e861cd5a1578905a3b87e32926b0b6da60950508 Mon Sep 17 00:00:00 2001 From: Glen Maddern Date: Tue, 6 Sep 2016 15:41:14 +1000 Subject: [PATCH 3/3] this is better, now only look for 200.html if we were going to look for 404.html --- lib/ecstatic.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/ecstatic.js b/lib/ecstatic.js index 1c07bbe..e7c1424 100755 --- a/lib/ecstatic.js +++ b/lib/ecstatic.js @@ -124,10 +124,17 @@ var ecstatic = module.exports = function (dir, options) { url: parsed.pathname + '.' + defaultExt + ((parsed.search)? parsed.search:'') }, res, next); } + 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); }