Permalink
Browse files

Prioritised default document (not just index.html)

  • Loading branch information...
1 parent 7b13854 commit fb0d798d804f8cd56c54f986a71c599fe23f571d @stoive committed Nov 22, 2011
Showing with 19 additions and 5 deletions.
  1. +19 −5 lib/middleware/static.js
View
@@ -90,6 +90,7 @@ var send = exports.send = function(req, res, next, options){
, getOnly = options.getOnly
, fn = options.callback
, hidden = options.hidden
+ , defaults = options.defaults
, done;
// replace next() with callback when available
@@ -133,11 +134,24 @@ var send = exports.send = function(req, res, next, options){
: next(err);
// redirect directory in case index.html is present
} else if (stat.isDirectory()) {
- if (!redirect) return next();
- res.statusCode = 301;
- res.setHeader('Location', url.pathname + '/');
- res.end('Redirecting to ' + url.pathname + '/');
- return;
+ if (redirect) {
+ res.statusCode = 301;
+ res.setHeader('Location', url.pathname + '/');
+ res.end('Redirecting to ' + url.pathname + '/');
+ return;
+ }
+ else if (defaults) {
+ fs.readdir(path, function(err, dir) {
+ if (err) return next();
+ var matches = dir.every(function(curr) { return ~defaults.indexOf(curr); });
+ if (matches) {
+ req.path = join(path, matches[0]);
+ send(req, res, next);
+ return;
+ }
+ }
+ }
+ else return next();
}
// header fields

0 comments on commit fb0d798

Please sign in to comment.