diff --git a/README.md b/README.md index caeb7f50..9308eb4a 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,25 @@ provided locals: Display mode. `tiles` and `details` are available. Defaults to `tiles`. +##### fallthrough + +Set the middleware to have client errors fall-through as just unhandled +requests, otherwise forward a client error. The difference is that client +errors like a bad request or a request to a non-existent file will cause +this middleware to simply `next()` to your next middleware when this value +is `true`. When this value is `false`, these errors (even 404s), will invoke +`next(err)`. + +Typically `true` is desired such that multiple physical directories can be +mapped to the same web address or for routes to fill in non-existent files. + +The value `false` can be used if this middleware is mounted at a path that +is designed to be strictly a single file system directory, which allows for +short-circuiting 404s for less overhead. This middleware will also reply to +all methods. + +The default value is `true`. + ## Examples ### Serve directory indexes with vanilla node.js http server diff --git a/index.js b/index.js index 7b9d856c..ddb7ebb9 100644 --- a/index.js +++ b/index.js @@ -97,9 +97,14 @@ function serveIndex(root, options) { var stylesheet = opts.stylesheet || defaultStylesheet; var template = opts.template || defaultTemplate; var view = opts.view || 'tiles'; + var fallthrough = opts.fallthrough !== false; return function (req, res, next) { if (req.method !== 'GET' && req.method !== 'HEAD') { + if (fallthrough) { + return next(); + } + res.statusCode = 'OPTIONS' === req.method ? 200 : 405; res.setHeader('Allow', 'GET, HEAD, OPTIONS'); res.setHeader('Content-Length', '0');