diff options
| author | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-06 19:08:07 -0400 |
|---|---|---|
| committer | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-06 19:08:07 -0400 |
| commit | 21167ce88ea4ad594d213d3a49115f0ebbaed745 (patch) | |
| tree | d337a8aa37223f4cc57d9ccbee7726914a530917 /app | |
| parent | 50c06cc74b025e98ebccc90d389d42354f2b2d63 (diff) | |
Fix god forsaken trailing slashed issue
Diffstat (limited to 'app')
| -rw-r--r-- | app/index.js | 22 | ||||
| -rw-r--r-- | app/node_modules/okserver/index.js | 20 | ||||
| -rw-r--r-- | app/node_modules/okview/index.js | 13 |
3 files changed, 32 insertions, 23 deletions
diff --git a/app/index.js b/app/index.js index a639477..b8a3333 100644 --- a/app/index.js +++ b/app/index.js @@ -1,5 +1,8 @@ var path = require('path'); var format = require('util').format; +var withTrailingSlash = require('okutil').withTrailingSlash; +var withoutTrailingSlash = require('okutil').withoutTrailingSlash; +var assign = require('object-assign'); var express = require('express'); var OKQuery = require('okquery'); var OKView = require('okview'); @@ -104,8 +107,9 @@ OKCMS.prototype._createViews = function(viewConfig, db, } var queryConfig = config.data || []; var queries = createQueries(queryConfig, resourceCache); - // Instantiate! - cache[route] = OKView({ + // Don't forget to add that trailing slash if the user forgot + cache[withTrailingSlash(route)] = OKView({ + mount: 'get', // User defined views are read only route: route, template: template, queries: queries, @@ -148,20 +152,6 @@ OKCMS.prototype._createQueries = function(queryConfig, resourceCache) { }); }; -OKCMS.prototype._initViews = function(server, views) { - Object.keys(views) - // Make sure more specific routes are processed first - // TODO This is not semantically correct (bro) - // Will prob manifest bugs - .sort(function(a, b) { - return a.length < b.length; - }) - // Add the views - .forEach(function(route) { - server.addView(route, views[route]); - }); -}; - module.exports = { createApp: function(options) { diff --git a/app/node_modules/okserver/index.js b/app/node_modules/okserver/index.js index a3219fa..6c19a9b 100644 --- a/app/node_modules/okserver/index.js +++ b/app/node_modules/okserver/index.js @@ -13,16 +13,24 @@ function OKServer(options) { var views = options.views; var express = options.express; var app = this._app = options.app; + var router = express.Router({ + strict: app.get('strict routing') + }); Object.keys(views) + // Sort such that more general routes are matched last .sort(specificity) + // Add the views .forEach(function(route) { - // We want to enforce trailing slashes for middleware - routeNoSlash = route.charAt(route.length - 1) === '/' ? - route.slice(0, route.length - 1) : route; - app.all(routeNoSlash, redirect(routeNoSlash)); - app.use(routeNoSlash + '/', views[route].middleware()); + var view = views[route]; + var mount = view.mount; + if (!mount) + throw Error('View doesn\'t specify a mount point'); + var handler = view.middleware(); + if (!handler) + throw new Error('View doesn\'t provide middleware'); + router[mount](route, handler); }); - // This enforces trailing slashes for stuff that isn't middleware + app.use(router); app.use(slash()); /** diff --git a/app/node_modules/okview/index.js b/app/node_modules/okview/index.js index 5f3c65c..1ceac03 100644 --- a/app/node_modules/okview/index.js +++ b/app/node_modules/okview/index.js @@ -20,7 +20,8 @@ function OKView(options) { throw new Error('No meta resource provided to view'); if (!options.route) throw new Error('No route provided to view'); - this.route = options.route; + var route = options.route; + var mount = options.mount || 'get'; this._template = options.template; var meta = this._meta = options.meta; var queries = this._queries = options.queries || []; @@ -29,6 +30,16 @@ function OKView(options) { // TODO This bound / unbound thing can probably be expressed in a // less convoluted way. var unbound = this.unbound = !!UNBOUND_ROUTE_PATTERN.exec(this.route); + Object.defineProperty(this, 'mount', { + value: mount, + writable: false, + enumerable: true + }); + Object.defineProperty(this, 'route', { + value: route, + writable: false, + enumerable: true + }); this._middleware = createMiddleware(this); this._fetchTemplateData = unbound ? fetchResourceTemplateData : fetchCollectionTemplateData; |
