router: refactor location of try blocks
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 8dc67af · Browse files · 4 files·+134 added−80 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+134 added−80 removed
- other:+134 added−80 removed
Provenance
- Commit
76e8bfa- Tree
746a0eb- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
8dc67af
4 files changed+134 added−80 removed
modified:lib/router/index.js+65 added-50 removed
154 unmodified lines155 156 next();157 158 function next(err) {159- if (err === 'route') {160- err = undefined;161- }159+ var layerError = err === 'route'160+ ? null161+ : err;162 163 var layer = stack[idx++];164- var layerPath;164 165 if (slashAdded) {166 req.url = req.url.substr(1);167 slashAdded = false;5 unmodified lines173 removed = '';174 }175 176 if (!layer) {178- return done(err);177+ return done(layerError);178 }179 181- try {182- var path = parseUrl(req).pathname;180+ self.match_layer(layer, req, res, function (err, path) {181+ if (err || path === undefined) {182+ return next(layerError || err);183+ }184 184- if (!layer.match(path)) return next(err);185-185 // route object and not middleware186 var route = layer.route;187 188 // if final route, then we support options189 if (route) {190 // we don't run any routes with error first192- if (err) {193- return next(err);191+ if (layerError) {192+ return next(layerError);193 }194 195 var method = req.method;196 var has_method = route._handles_method(method);15 unmodified lines212 // Capture one-time layer values213 req.params = self.mergeParams214 ? mergeParams(layer.params, parentParams)215 : layer.params;217- layerPath = layer.path;216+ var layerPath = layer.path;217 218 // this should be done for the layer220- return self.process_params(layer, paramcalled, req, res, function(err) {219+ self.process_params(layer, paramcalled, req, res, function (err) {220 if (err) {222- return next(err);221+ return next(layerError || err);222 }223 224 if (route) {226- return layer.handle(req, res, next);225+ return layer.handle_request(req, res, next);226 }227 229- trim_prefix();228+ trim_prefix(layer, layerError, layerPath, path);229 });230+ });231+ }232 232- } catch (err) {233- next(err);233+ function trim_prefix(layer, layerError, layerPath, path) {234+ var c = path[layerPath.length];235+ if (c && '/' !== c && '.' !== c) return next(layerError);236+237+ // Trim off the part of the url that matches the route238+ // middleware (.use stuff) needs to have the path stripped239+ if (layerPath.length !== 0) {240+ debug('trim prefix (%s) from url %s', layerPath, req.url);241+ removed = layerPath;242+ req.url = protohost + req.url.substr(protohost.length + removed.length);243+244+ // Ensure leading slash245+ if (!fqdn && req.url[0] !== '/') {246+ req.url = '/' + req.url;247+ slashAdded = true;248+ }249+250+ // Setup base URL (no trailing slash)251+ req.baseUrl = parentUrl + (removed[removed.length - 1] === '/'252+ ? removed.substring(0, removed.length - 1)253+ : removed);254 }255 236- function trim_prefix() {237- var c = path[layerPath.length];238- if (c && '/' != c && '.' != c) return next(err);256+ debug('%s %s : %s', layer.name, layerPath, req.originalUrl);257 240- // Trim off the part of the url that matches the route241- // middleware (.use stuff) needs to have the path stripped242- if (layerPath.length !== 0) {243- debug('trim prefix (%s) from url %s', layerPath, req.url);244- removed = layerPath;245- req.url = protohost + req.url.substr(protohost.length + removed.length);258+ if (layerError) {259+ layer.handle_error(layerError, req, res, next);260+ } else {261+ layer.handle_request(req, res, next);262+ }263+ }264+};265 247- // Ensure leading slash248- if (!fqdn && req.url[0] !== '/') {249- req.url = '/' + req.url;250- slashAdded = true;251- }266+/**267+ * Match request to a layer.268+ *269+ * @api private270+ */271 253- // Setup base URL (no trailing slash)254- req.baseUrl = parentUrl + (removed[removed.length - 1] === '/'255- ? removed.substring(0, removed.length - 1)256- : removed);257- }272+proto.match_layer = function match_layer(layer, req, res, done) {273+ var error = null;274+ var path;275 259- debug('%s %s : %s', layer.handle.name || 'anonymous', layerPath, req.originalUrl);260- var arity = layer.handle.length;261- try {262- if (err && arity === 4) {263- layer.handle(err, req, res, next);264- } else if (!err && arity < 4) {265- layer.handle(req, res, next);266- } else {267- next(err);268- }269- } catch (err) {270- next(err);271- }276+ try {277+ path = parseUrl(req).pathname;278+279+ if (!layer.match(path)) {280+ path = undefined;281 }282+ } catch (err) {283+ error = err;284 }285+286+ done(error, path);287 };288 289 /**290 * Process any parameters for the layer.127 unmodified lines418 strict: false,419 end: false420 }, fn);421 422+ layer.route = undefined;423+424 // add the middleware425 debug('use %s %s', path || '/', fn.name || 'anonymous');426 427 this.stack.push(layer);109 unmodified linesmodified:lib/router/layer.js+50 added
18 unmodified lines1920212223242526272 unmodified lines30313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858665 unmodified lines18 unmodified lines debug('new %s', path); options = options || {};
this.handle = fn; this.name = fn.name || '<anonymous>'; this.params = undefined; this.path = undefined; this.regexp = pathRegexp(path, this.keys = [], options);
2 unmodified lines }}
/** * Handle the error for the layer. * * @param {Error} error * @param {Request} req * @param {Response} res * @param {function} next * @api private */
Layer.prototype.handle_error = function handle_error(error, req, res, next) { var fn = this.handle;
if (fn.length !== 4) { // not a standard error handler return next(error); }
try { fn(error, req, res, next); } catch (err) { next(err); }};
/** * Handle the request for the layer. * * @param {Request} req * @param {Response} res * @param {function} next * @api private */
Layer.prototype.handle_request = function handle(req, res, next) { var fn = this.handle;
if (fn.length > 3) { // not a standard request handler return next(); }
try { fn(req, res, next); } catch (err) { next(err); }};
/** * Check if this route matches `path`, if so * populate `.params`. * * @param {String} path65 unmodified linesmodified:lib/router/route.js+18 added-30 removed
1 unmodified line2 * Module dependencies.3 */4 5 var debug = require('debug')('express:router:route');6+var Layer = require('./layer');7 var methods = require('methods');8 var utils = require('../utils');9 10 /**53 unmodified lines64 * @api private65 */66 67 Route.prototype.dispatch = function(req, res, done){68+ var idx = 0;69 var stack = this.stack;70 if (stack.length === 0) {71 return done();72 }4 unmodified lines77 }78 79 req.route = this;80 79- var idx = 0;80- (function next_layer(err) {81+ next();82+83+ function next(err) {84 if (err && err === 'route') {85 return done();86 }87 2 unmodified lines90 return done(err);91 }92 93 if (layer.method && layer.method !== method) {91- return next_layer(err);94+ return next(err);95 }96 94- var arity = layer.handle.length;97 if (err) {96- if (arity < 4) {97- return next_layer(err);98- }99-100- try {101- layer.handle(err, req, res, next_layer);102- } catch (err) {103- next_layer(err);104- }105- return;98+ layer.handle_error(err, req, res, next);99+ } else {100+ layer.handle_request(req, res, next);101 }107-108- if (arity > 3) {109- return next_layer();110- }102+ }111-112- try {113- layer.handle(req, res, next_layer);114- } catch (err) {115- next_layer(err);116- }117- })();103 };104 105 /**106 * Add a handler for all HTTP verbs to this route.32 unmodified lines139 var msg = 'Route.all() requires callback functions but got a ' + type;140 throw new Error(msg);141 }142 143+ var layer = Layer('/', {}, fn);144+ layer.method = undefined;145+146 self.methods._all = true;159- self.stack.push({ handle: fn });147+ self.stack.push(layer);148 });149 150 return self;151 };11 unmodified lines163 }164 165 debug('%s %s', method, self.path);166 179- if (!self.methods[method]) {180- self.methods[method] = true;181- }167+ var layer = Layer('/', {}, fn);168+ layer.method = method;169 183- self.stack.push({ method: method, handle: fn });170+ self.methods[method] = true;171+ self.stack.push(layer);172 });173 return self;174 };175 });modified:History.md+1 added
8 unmodified lines9 * support non-string `path` in `app.use(path, fn)`10 - supports array of paths11 - supports `RegExp`12 * router: fix optimization on router exit13+ * router: refactor location of `try` blocks14 * router: speed up standard `app.use(fn)`15 * deps: debug@1.0.316 - Add support for multiple wildcards in namespaces17 * deps: methods@1.1.01776 unmodified lines