route.js
This file
Path
lib/router/route.js
193 lines · 3.7 KiB · JavaScript
Last changed by
perf: enable strict modeDouglas Christopher Wilsonbefore adoptione71014f
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: add license commentsDouglas Christopher Wilsonbefore adoption95ad276
lines 1–8why, one step back1/*!2 * express3 * Copyright(c) 2009-2013 TJ Holowaychuk4 * Copyright(c) 2013 Roman Shtylman5 * Copyright(c) 2014-2015 Douglas Christopher Wilson6 * MIT Licensed7 */8perf: enable strict modeDouglas Christopher Wilsonbefore adoptione71014f
lines 9–10why, one step back9'use strict';10docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 11–15why, one step back11/**12 * Module dependencies.13 */1415var debug = require('debug')('express:router:route');Use array-flatten module for flattening arraysDeepak Kapoorbefore adoptionf7e94a3
line 16why, one step back16var flatten = require('array-flatten');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 17–64why, one step back17var Layer = require('./layer');18var methods = require('methods');1920/**21 * Expose `Route`.22 */2324module.exports = Route;2526/**27 * Initialize `Route` with the given `path`,28 *29 * @param {String} path30 * @api private31 */3233function Route(path) {34 debug('new %s', path);35 this.path = path;36 this.stack = [];3738 // route handlers for various http methods39 this.methods = {};40}4142/**43 * @api private44 */4546Route.prototype._handles_method = function _handles_method(method) {47 if (this.methods._all) {48 return true;49 }5051 method = method.toLowerCase();5253 if (method === 'head' && !this.methods['head']) {54 method = 'get';55 }5657 return Boolean(this.methods[method]);58};5960/**61 * @return {Array} supported HTTP methods62 * @api private63 */64Fix OPTIONS responses to include the HEAD method properlyDouglas Christopher Wilsonbefore adoption935f05b
lines 65–78why, one step back65Route.prototype._options = function _options() {66 var methods = Object.keys(this.methods);6768 // append automatic head69 if (this.methods.get && !this.methods.head) {70 methods.push('head');71 }7273 for (var i = 0; i < methods.length; i++) {74 // make upper case75 methods[i] = methods[i].toUpperCase();76 }7778 return methods;docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 79–153why, one step back79};8081/**82 * dispatch req, res into this route83 *84 * @api private85 */8687Route.prototype.dispatch = function(req, res, done){88 var idx = 0;89 var stack = this.stack;90 if (stack.length === 0) {91 return done();92 }9394 var method = req.method.toLowerCase();95 if (method === 'head' && !this.methods['head']) {96 method = 'get';97 }9899 req.route = this;100101 next();102103 function next(err) {104 if (err && err === 'route') {105 return done();106 }107108 var layer = stack[idx++];109 if (!layer) {110 return done(err);111 }112113 if (layer.method && layer.method !== method) {114 return next(err);115 }116117 if (err) {118 layer.handle_error(err, req, res, next);119 } else {120 layer.handle_request(req, res, next);121 }122 }123};124125/**126 * Add a handler for all HTTP verbs to this route.127 *128 * Behaves just like middleware and can respond or call `next`129 * to continue processing.130 *131 * You can use multiple `.all` call to add multiple handlers.132 *133 * function check_something(req, res, next){134 * next();135 * };136 *137 * function validate_user(req, res, next){138 * next();139 * };140 *141 * route142 * .all(validate_user)143 * .all(check_something)144 * .get(function(req, res, next){145 * res.send('hello world');146 * });147 *148 * @param {function} handler149 * @return {Route} for chaining150 * @api public151 */152153Route.prototype.all = function(){Use array-flatten module for flattening arraysDeepak Kapoorbefore adoptionf7e94a3
line 154why, one step back154 var callbacks = flatten([].slice.call(arguments));docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 155–164why, one step back155 callbacks.forEach(function(fn) {156 if (typeof fn !== 'function') {157 var type = {}.toString.call(fn);158 var msg = 'Route.all() requires callback functions but got a ' + type;159 throw new Error(msg);160 }161162 var layer = Layer('/', {}, fn);163 layer.method = undefined;164Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
lines 165–167why, one step back165 this.methods._all = true;166 this.stack.push(layer);167 }, this);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
line 168why, one step back168Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
line 169why, one step back169 return this;docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 170–173why, one step back170};171172methods.forEach(function(method){173 Route.prototype[method] = function(){Use array-flatten module for flattening arraysDeepak Kapoorbefore adoptionf7e94a3
line 174why, one step back174 var callbacks = flatten([].slice.call(arguments));docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 175–182why, one step back175176 callbacks.forEach(function(fn) {177 if (typeof fn !== 'function') {178 var type = {}.toString.call(fn);179 var msg = 'Route.' + method + '() requires callback functions but got a ' + type;180 throw new Error(msg);181 }182Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
line 183why, one step back183 debug('%s %s', method, this.path);docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 184–187why, one step back184185 var layer = Layer('/', {}, fn);186 layer.method = method;187Pass context to .forEach instead of closurelemmybefore adoptiondfa7ee4
lines 188–191why, one step back188 this.methods[method] = true;189 this.stack.push(layer);190 }, this);191 return this;docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 192–193why, one step back192 };193});