Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactor routeParams and params property creation to be done in the r…

…equest constructor

move logic to get request parameters for a route into a method on the route
  • Loading branch information...
commit b6f8a9c43342b1fe27e65a77e1f70b633e48d021 1 parent 363faac
Nathan authored

Showing 2 changed files with 37 additions and 20 deletions. Show diff stats Hide diff stats

  1. +9 0 lib/request.js
  2. +28 20 lib/router.js
9 lib/request.js
@@ -13,6 +13,15 @@ module.exports = function(router, jsgiReq) {
13 13 get: function() {
14 14 return !util.no(this.headers['x-requested-with']);
15 15 }
  16 + },
  17 + routeParams: {
  18 + value: {},
  19 + enumerable: true
  20 + },
  21 + params: {
  22 + get: function() {
  23 + return util.merge({}, this.routeParams, this.search, this.body);
  24 + }
16 25 }
17 26 });
18 27 };
48 lib/router.js
@@ -145,7 +145,7 @@ Router.prototype.route = function(method, path /*, handlers... */) {
145 145 path = new RegExp("^"+path.replace(/\./, '\\.').replace(/\*/g, '(.+)').replace(PATH_PARAMETERS, PATH_PARAMETER_REPLACEMENT)+'$');
146 146 }
147 147
148   - route = { path: path, paramNames: paramNames, apps: apps, originalPath: originalPath };
  148 + route = makeRoute({ path: path, paramNames: paramNames, apps: apps, originalPath: originalPath });
149 149
150 150 this.emit(exports.bogartEvent.BEFORE_ADD_ROUTE, this, route);
151 151
@@ -182,31 +182,13 @@ Router.prototype.respond = function(reqPromise) {
182 182
183 183 return when(reqPromise, function(req) {
184 184 var route = self.handler(req.method, req.pathInfo)
185   - , routeParams = {}
186 185 , routeParamValues = null;
187 186
188 187 if (util.no(route)) {
189 188 return null;
190 189 }
191 190
192   - routeParamValues = route.path.exec(req.pathInfo);
193   - if (routeParamValues) {
194   - routeParamValues.shift(); // Remove the initial match
195   -
196   - routeParamValues.forEach(function(val, indx) {
197   - val = decodeURIComponent(val);
198   - if (route.paramNames && route.paramNames.length > indx) {
199   - routeParams[route.paramNames[indx]] = val;
200   - } else if (val !== undefined) {
201   - routeParams.splat = routeParams.splat || [];
202   - routeParams.splat.push(val);
203   - }
204   - });
205   - }
206   -
207   - Object.defineProperty(req, 'routeParams', { value: routeParams, enumerable: true, readonly: true });
208   -
209   - Object.defineProperty(req, 'params', { value: util.merge({}, req.routeParams, req.search, req.body), enumerable: true, readonly: true });
  191 + routeParamValues = route.getParamValuesFromRequest(req);
210 192
211 193 /* Invoke all 'before' functions and save any promises. */
212 194 var promiseArray = [];
@@ -293,4 +275,30 @@ function thenResolve(val) {
293 275 return function() {
294 276 return val;
295 277 }
  278 +}
  279 +
  280 +function makeRoute(proto) {
  281 + return Object.create(proto, {
  282 + getParamValuesFromRequest: {
  283 + value: function(req) {
  284 + var route = this
  285 + , routeParamValues = route.path.exec(req.pathInfo);
  286 +
  287 + if (routeParamValues) {
  288 + routeParamValues.shift(); // Remove the initial match
  289 +
  290 + routeParamValues
  291 + .map(decodeURIComponent)
  292 + .forEach(function(val, indx) {
  293 + if (route.paramNames && route.paramNames.length > indx) {
  294 + req.routeParams[route.paramNames[indx]] = val;
  295 + } else if (val !== undefined) {
  296 + req.routeParams.splat = req.routeParams.splat || [];
  297 + req.routeParams.splat.push(val);
  298 + }
  299 + });
  300 + }
  301 + }
  302 + }
  303 + });
296 304 }

0 comments on commit b6f8a9c

Please sign in to comment.
Something went wrong with that request. Please try again.