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

make directory redirects optional #398

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 12 additions & 3 deletions lib/middleware/static.js
Expand Up @@ -53,6 +53,11 @@ exports = module.exports = function static(root, options){
if (!root) throw new Error('static() root path required');
options.root = root;

// defaults
if (options["redirectDirectories"] == null) {
options["redirectDirectories"] = true;
}

return function static(req, res, next) {
options.path = req.url;
options.getOnly = true;
Expand Down Expand Up @@ -134,9 +139,13 @@ var send = exports.send = function(req, res, next, options){
: next(err);
// redirect directory in case index.html is present
} else if (stat.isDirectory()) {
res.statusCode = 301;
res.setHeader('Location', url.pathname + '/');
res.end('Redirecting to ' + url.pathname + '/');
if (options.redirectDirectories) {
res.statusCode = 301;
res.setHeader('Location', url.pathname + '/');
res.end('Redirecting to ' + url.pathname + '/');
} else {
next()
}
return;
}

Expand Down