diff options
Diffstat (limited to 'app/node_modules/okserver/index.js')
| -rw-r--r-- | app/node_modules/okserver/index.js | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/app/node_modules/okserver/index.js b/app/node_modules/okserver/index.js index 230a53c..d665d65 100644 --- a/app/node_modules/okserver/index.js +++ b/app/node_modules/okserver/index.js @@ -1,5 +1,6 @@ var specificity = require('route-order')(); var express = require('express'); +var slash = require('express-slash'); function OKServer(options) { if (!(this instanceof OKServer)) return new OKServer(options); @@ -8,12 +9,33 @@ function OKServer(options) { throw new Error('No views provided to OKServer!'); var views = options.views; var app = this._app = express(); + app.enable('strict routing'); + var router = express.Router({ + strict: app.get('strict routing') + }); // Add views Object.keys(views) .sort(specificity) .forEach(function(route) { - app.use(route, views[route].middleware()); + // We want to enforce trailing slashes for middleware + routeNoSlash = route.charAt(route.length - 1) === '/' ? + route.slice(0, route.length - 1) : route; + router.all(routeNoSlash, redirect(routeNoSlash)); + router.use(routeNoSlash + '/', views[route].middleware()); }); + app.use(router); + // This enforces trailing slashes for stuff that isn't middleware + app.use(slash()); + + /** + * Create a handler which redirect all requests to + * the same route with a trailing slash appended + */ + function redirect(routeNoSlash) { + return function(req, res) { + res.redirect(301, routeNoSlash + '/'); + } + } } OKServer.prototype.listen = function listen(port) { |
