Skip to content

Commit

Permalink
Add fallbackFile option to response for unhandled requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ngs committed Oct 16, 2015
1 parent 160b756 commit b55026b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ To allow fall through to your custom routes:
ecstatic({ root: __dirname + '/public', handleError: false })
```

### fallback file
To response with specified file:

```js
ecstatic({ root: __dirname + '/public', fallbackFile: 'index.html' })
```

# API:

## ecstatic(opts);
Expand All @@ -76,14 +83,15 @@ var opts = {
serverHeader : true,
contentType : 'application/octet-stream',
mimeTypes : undefined,
fallbackFile : undefined
handleOptionsMethod: false
}
```

If `opts` is a string, the string is assigned to the root folder and all other
options are set to their defaults.

### `opts.root`
### `opts.root`

`opts.root` is the directory you want to serve up.

Expand Down Expand Up @@ -160,7 +168,7 @@ Defaults to **application/octet-stream**.

Add new or override one or more mime-types. This affects the HTTP Content-Type header.
Can either be a path to a [`.types`](http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types) file or an object hash of type(s).

ecstatic({ mimeType: { 'mime-type': ['file_extension', 'file_extension'] } })

### `opts.handleError`
Expand Down
8 changes: 7 additions & 1 deletion lib/ecstatic.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var ecstatic = module.exports = function (dir, options) {
handleError = opts.handleError,
headers = opts.headers,
serverHeader = opts.serverHeader,
fallbackFile = opts.fallbackFile;
weakEtags = opts.weakEtags,
handleOptionsMethod = opts.handleOptionsMethod;

Expand Down Expand Up @@ -108,6 +109,11 @@ var ecstatic = module.exports = function (dir, options) {
url: parsed.pathname + '.' + defaultExt + ((parsed.search)? parsed.search:'')
}, res, next);
}
else if (fallbackFile) {
middleware({
url: ('/' + path.join(baseDir, fallbackFile))
}, res, next);
}
else {
// Try to serve default ./404.html
middleware({
Expand Down Expand Up @@ -273,7 +279,7 @@ var ecstatic = module.exports = function (dir, options) {
}

if (clientEtag) {
if (opts.weakCompare && clientEtag !== serverEtag
if (opts.weakCompare && clientEtag !== serverEtag
&& clientEtag !== ('W/' + serverEtag) && ('W/' + clientEtag) !== serverEtag) {
return false;
} else if (!opts.weakCompare && (clientEtag !== serverEtag || clientEtag.indexOf('W/') === 0)) {
Expand Down
5 changes: 5 additions & 0 deletions lib/ecstatic/aliases.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,10 @@
"handleOptionsMethod",
"handleoptionsmethod",
"handle-options-method"
],
"fallbackFile": [
"fallbackFile",
"fallbackfile",
"fallback-file"
]
}
9 changes: 9 additions & 0 deletions lib/ecstatic/opts.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = function (opts) {
headers = {},
serverHeader = defaults.serverHeader,
contentType = defaults.contentType,
fallbackFile,
mimeTypes,
weakEtags = defaults.weakEtags,
weakCompare = defaults.weakCompare,
Expand Down Expand Up @@ -104,6 +105,13 @@ module.exports = function (opts) {
}
});

aliases.fallbackFile.some(function (k) {
if (isDeclared(k)) {
fallbackFile = opts[k];
return true;
}
});

aliases.serverHeader.some(function (k) {
if (isDeclared(k)) {
serverHeader = opts[k];
Expand Down Expand Up @@ -158,6 +166,7 @@ module.exports = function (opts) {
gzip: gzip,
handleError: handleError,
headers: headers,
fallbackFile: fallbackFile,
serverHeader: serverHeader,
contentType: contentType,
mimeTypes: mimeTypes,
Expand Down
29 changes: 29 additions & 0 deletions test/custom-fallback-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var test = require('tap').test,
http = require('http'),
request = require('request'),
ecstatic = require('../');

test('custom fallbackFile', function(t) {
try {
var server = http.createServer(ecstatic({
root: __dirname + '/public/',
fallbackFile: 'a.txt'
}));
} catch (e) {
t.fail(e.message);
t.end();
}

t.plan(4);

server.listen(0, function() {
var port = server.address().port;
request.get('http://localhost:' + port + '/file-not-found', function(err, res, body) {
t.ifError(err);
t.equal(res.statusCode, 200, 'status code should be ok');
t.equal(res.headers['content-type'], 'text/plain; charset=UTF-8');
server.close(function() { t.end(); });
t.equal('A!!!\n', body);
});
});
});

0 comments on commit b55026b

Please sign in to comment.