diff options
| author | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-06 13:14:41 -0400 |
|---|---|---|
| committer | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-06 15:27:53 -0400 |
| commit | 4c83c4a5da14c81bc74b8e9ce196e0c9017c89cc (patch) | |
| tree | 1e030531331a65235a82a6f45cb5773c92853ed2 /app/node_modules/okserver/index.js | |
| parent | 2175598ab95f33b3779d83e2df433eeedd25e70c (diff) | |
Enforce trailing slashes
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) { |
