Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallbackFile option to response for unhandled requests #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});
});