Use router module for routing
Before adoption: this commit came in with the history the project adopted, so no landing records it.
closes #2411
authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 21d52da · Browse files · 11 files·+29 added−1039 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+29 added−1039 removed
- tests:+24 added−23 removed
- other:+5 added−1016 removed
Provenance
- Commit
cec5780- Tree
eea2800- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
21d52da
11 files changed+29 added−1039 removed
deleted:lib/router/index.js-626 removed
1-/*!2- * express3- * Copyright(c) 2009-2013 TJ Holowaychuk4- * Copyright(c) 2013 Roman Shtylman5- * Copyright(c) 2014-2015 Douglas Christopher Wilson6- * MIT Licensed7- */8-9-'use strict';10-11-/**12- * Module dependencies.13- * @private14- */15-16-var Route = require('./route');17-var Layer = require('./layer');18-var methods = require('methods');19-var mixin = require('utils-merge');20-var debug = require('debug')('express:router');21-var flatten = require('array-flatten');22-var parseUrl = require('parseurl');23-24-/**25- * Module variables.26- * @private27- */28-29-var objectRegExp = /^\[object (\S+)\]$/;30-var slice = Array.prototype.slice;31-var toString = Object.prototype.toString;32-33-/**34- * Initialize a new `Router` with the given `options`.35- *36- * @param {Object} options37- * @return {Router} which is an callable function38- * @public39- */40-41-var proto = module.exports = function(options) {42- var opts = options || {};43-44- function router(req, res, next) {45- router.handle(req, res, next);46- }47-48- // mixin Router class functions49- router.__proto__ = proto;50-51- router.params = {};52- router.caseSensitive = opts.caseSensitive;53- router.mergeParams = opts.mergeParams;54- router.strict = opts.strict;55- router.stack = [];56-57- return router;58-};59-60-/**61- * Map the given param placeholder `name`(s) to the given callback.62- *63- * Parameter mapping is used to provide pre-conditions to routes64- * which use normalized placeholders. For example a _:user_id_ parameter65- * could automatically load a user's information from the database without66- * any additional code,67- *68- * The callback uses the same signature as middleware, the only difference69- * being that the value of the placeholder is passed, in this case the _id_70- * of the user. Once the `next()` function is invoked, just like middleware71- * it will continue on to execute the route, or subsequent parameter functions.72- *73- * Just like in middleware, you must either respond to the request or call next74- * to avoid stalling the request.75- *76- * app.param('user_id', function(req, res, next, id){77- * User.find(id, function(err, user){78- * if (err) {79- * return next(err);80- * } else if (!user) {81- * return next(new Error('failed to load user'));82- * }83- * req.user = user;84- * next();85- * });86- * });87- *88- * @param {String} name89- * @param {Function} fn90- * @return {app} for chaining91- * @public92- */93-94-proto.param = function param(name, fn) {95- if (!fn) {96- throw new TypeError('argument fn is required');97- }98-99- if (typeof fn !== 'function') {100- throw new TypeError('argument fn must be a function');101- }102-103- var params = this.params[name];104-105- if (!params) {106- params = this.params[name] = [];107- }108-109- params.push(fn);110-111- return this;112-};113-114-/**115- * Dispatch a req, res into the router.116- * @private117- */118-119-proto.handle = function handle(req, res, out) {120- var self = this;121-122- debug('dispatching %s %s', req.method, req.url);123-124- var search = 1 + req.url.indexOf('?');125- var pathlength = search ? search - 1 : req.url.length;126- var fqdn = req.url[0] !== '/' && 1 + req.url.substr(0, pathlength).indexOf('://');127- var protohost = fqdn ? req.url.substr(0, req.url.indexOf('/', 2 + fqdn)) : '';128- var idx = 0;129- var removed = '';130- var slashAdded = false;131- var paramcalled = {};132-133- // store options for OPTIONS request134- // only used if OPTIONS request135- var options = [];136-137- // middleware and routes138- var stack = self.stack;139-140- // manage inter-router variables141- var parentParams = req.params;142- var parentUrl = req.baseUrl || '';143- var done = restore(out, req, 'baseUrl', 'next', 'params');144-145- // setup next layer146- req.next = next;147-148- // for options requests, respond with a default if nothing else responds149- if (req.method === 'OPTIONS') {150- done = wrap(done, function(old, err) {151- if (err || options.length === 0) return old(err);152- sendOptionsResponse(res, options, old);153- });154- }155-156- // setup basic req values157- req.baseUrl = parentUrl;158- req.originalUrl = req.originalUrl || req.url;159-160- next();161-162- function next(err) {163- var layerError = err === 'route'164- ? null165- : err;166-167- // remove added slash168- if (slashAdded) {169- req.url = req.url.substr(1);170- slashAdded = false;171- }172-173- // restore altered req.url174- if (removed.length !== 0) {175- req.baseUrl = parentUrl;176- req.url = protohost + removed + req.url.substr(protohost.length);177- removed = '';178- }179-180- // no more matching layers181- if (idx >= stack.length) {182- setImmediate(done, layerError);183- return;184- }185-186- // get pathname of request187- var path = getPathname(req);188-189- if (path == null) {190- return done(layerError);191- }192-193- // find next matching layer194- var layer;195- var match;196- var route;197-198- while (match !== true && idx < stack.length) {199- layer = stack[idx++];200- match = matchLayer(layer, path);201- route = layer.route;202-203- if (typeof match !== 'boolean') {204- // hold on to layerError205- layerError = layerError || match;206- }207-208- if (match !== true) {209- continue;210- }211-212- if (!route) {213- // process non-route handlers normally214- continue;215- }216-217- if (layerError) {218- // routes do not match with a pending error219- match = false;220- continue;221- }222-223- var method = req.method;224- var has_method = route._handles_method(method);225-226- // build up automatic options response227- if (!has_method && method === 'OPTIONS') {228- appendMethods(options, route._options());229- }230-231- // don't even bother matching route232- if (!has_method && method !== 'HEAD') {233- match = false;234- continue;235- }236- }237-238- // no match239- if (match !== true) {240- return done(layerError);241- }242-243- // store route for dispatch on change244- if (route) {245- req.route = route;246- }247-248- // Capture one-time layer values249- req.params = self.mergeParams250- ? mergeParams(layer.params, parentParams)251- : layer.params;252- var layerPath = layer.path;253-254- // this should be done for the layer255- self.process_params(layer, paramcalled, req, res, function (err) {256- if (err) {257- return next(layerError || err);258- }259-260- if (route) {261- return layer.handle_request(req, res, next);262- }263-264- trim_prefix(layer, layerError, layerPath, path);265- });266- }267-268- function trim_prefix(layer, layerError, layerPath, path) {269- var c = path[layerPath.length];270- if (c && '/' !== c && '.' !== c) return next(layerError);271-272- // Trim off the part of the url that matches the route273- // middleware (.use stuff) needs to have the path stripped274- if (layerPath.length !== 0) {275- debug('trim prefix (%s) from url %s', layerPath, req.url);276- removed = layerPath;277- req.url = protohost + req.url.substr(protohost.length + removed.length);278-279- // Ensure leading slash280- if (!fqdn && req.url[0] !== '/') {281- req.url = '/' + req.url;282- slashAdded = true;283- }284-285- // Setup base URL (no trailing slash)286- req.baseUrl = parentUrl + (removed[removed.length - 1] === '/'287- ? removed.substring(0, removed.length - 1)288- : removed);289- }290-291- debug('%s %s : %s', layer.name, layerPath, req.originalUrl);292-293- if (layerError) {294- layer.handle_error(layerError, req, res, next);295- } else {296- layer.handle_request(req, res, next);297- }298- }299-};300-301-/**302- * Process any parameters for the layer.303- * @private304- */305-306-proto.process_params = function process_params(layer, called, req, res, done) {307- var params = this.params;308-309- // captured parameters from the layer, keys and values310- var keys = layer.keys;311-312- // fast track313- if (!keys || keys.length === 0) {314- return done();315- }316-317- var i = 0;318- var name;319- var paramIndex = 0;320- var key;321- var paramVal;322- var paramCallbacks;323- var paramCalled;324-325- // process params in order326- // param callbacks can be async327- function param(err) {328- if (err) {329- return done(err);330- }331-332- if (i >= keys.length ) {333- return done();334- }335-336- paramIndex = 0;337- key = keys[i++];338-339- if (!key) {340- return done();341- }342-343- name = key.name;344- paramVal = req.params[name];345- paramCallbacks = params[name];346- paramCalled = called[name];347-348- if (paramVal === undefined || !paramCallbacks) {349- return param();350- }351-352- // param previously called with same value or error occurred353- if (paramCalled && (paramCalled.match === paramVal354- || (paramCalled.error && paramCalled.error !== 'route'))) {355- // restore value356- req.params[name] = paramCalled.value;357-358- // next param359- return param(paramCalled.error);360- }361-362- called[name] = paramCalled = {363- error: null,364- match: paramVal,365- value: paramVal366- };367-368- paramCallback();369- }370-371- // single param callbacks372- function paramCallback(err) {373- var fn = paramCallbacks[paramIndex++];374-375- // store updated value376- paramCalled.value = req.params[key.name];377-378- if (err) {379- // store error380- paramCalled.error = err;381- param(err);382- return;383- }384-385- if (!fn) return param();386-387- try {388- fn(req, res, paramCallback, paramVal, key.name);389- } catch (e) {390- paramCallback(e);391- }392- }393-394- param();395-};396-397-/**398- * Use the given middleware function, with optional path, defaulting to "/".399- *400- * Use (like `.all`) will run for any http METHOD, but it will not add401- * handlers for those methods so OPTIONS requests will not consider `.use`402- * functions even if they could respond.403- *404- * The other difference is that _route_ path is stripped and not visible405- * to the handler function. The main effect of this feature is that mounted406- * handlers can operate without any code changes regardless of the "prefix"407- * pathname.408- *409- * @public410- */411-412-proto.use = function use(fn) {413- var offset = 0;414- var path = '/';415-416- // default path to '/'417- // disambiguate router.use([fn])418- if (typeof fn !== 'function') {419- var arg = fn;420-421- while (Array.isArray(arg) && arg.length !== 0) {422- arg = arg[0];423- }424-425- // first arg is the path426- if (typeof arg !== 'function') {427- offset = 1;428- path = fn;429- }430- }431-432- var callbacks = flatten(slice.call(arguments, offset));433-434- if (callbacks.length === 0) {435- throw new TypeError('Router.use() requires middleware functions');436- }437-438- for (var i = 0; i < callbacks.length; i++) {439- var fn = callbacks[i];440-441- if (typeof fn !== 'function') {442- throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));443- }444-445- // add the middleware446- debug('use %s %s', path, fn.name || '<anonymous>');447-448- var layer = new Layer(path, {449- sensitive: this.caseSensitive,450- strict: false,451- end: false452- }, fn);453-454- layer.route = undefined;455-456- this.stack.push(layer);457- }458-459- return this;460-};461-462-/**463- * Create a new Route for the given path.464- *465- * Each route contains a separate middleware stack and VERB handlers.466- *467- * See the Route api documentation for details on adding handlers468- * and middleware to routes.469- *470- * @param {String} path471- * @return {Route}472- * @public473- */474-475-proto.route = function route(path) {476- var route = new Route(path);477-478- var layer = new Layer(path, {479- sensitive: this.caseSensitive,480- strict: this.strict,481- end: true482- }, route.dispatch.bind(route));483-484- layer.route = route;485-486- this.stack.push(layer);487- return route;488-};489-490-// create Router#VERB functions491-methods.concat('all').forEach(function(method){492- proto[method] = function(path){493- var route = this.route(path)494- route[method].apply(route, slice.call(arguments, 1));495- return this;496- };497-});498-499-// append methods to a list of methods500-function appendMethods(list, addition) {501- for (var i = 0; i < addition.length; i++) {502- var method = addition[i];503- if (list.indexOf(method) === -1) {504- list.push(method);505- }506- }507-}508-509-// get pathname of request510-function getPathname(req) {511- try {512- return parseUrl(req).pathname;513- } catch (err) {514- return undefined;515- }516-}517-518-// get type for error message519-function gettype(obj) {520- var type = typeof obj;521-522- if (type !== 'object') {523- return type;524- }525-526- // inspect [[Class]] for objects527- return toString.call(obj)528- .replace(objectRegExp, '$1');529-}530-531-/**532- * Match path to a layer.533- *534- * @param {Layer} layer535- * @param {string} path536- * @private537- */538-539-function matchLayer(layer, path) {540- try {541- return layer.match(path);542- } catch (err) {543- return err;544- }545-}546-547-// merge params with parent params548-function mergeParams(params, parent) {549- if (typeof parent !== 'object' || !parent) {550- return params;551- }552-553- // make copy of parent for base554- var obj = mixin({}, parent);555-556- // simple non-numeric merging557- if (!(0 in params) || !(0 in parent)) {558- return mixin(obj, params);559- }560-561- var i = 0;562- var o = 0;563-564- // determine numeric gaps565- while (i === o || o in parent) {566- if (i in params) i++;567- if (o in parent) o++;568- }569-570- // offset numeric indices in params before merge571- for (i--; i >= 0; i--) {572- params[i + o] = params[i];573-574- // create holes for the merge when necessary575- if (i < o) {576- delete params[i];577- }578- }579-580- return mixin(parent, params);581-}582-583-// restore obj props after function584-function restore(fn, obj) {585- var props = new Array(arguments.length - 2);586- var vals = new Array(arguments.length - 2);587-588- for (var i = 0; i < props.length; i++) {589- props[i] = arguments[i + 2];590- vals[i] = obj[props[i]];591- }592-593- return function(err){594- // restore vals595- for (var i = 0; i < props.length; i++) {596- obj[props[i]] = vals[i];597- }598-599- return fn.apply(this, arguments);600- };601-}602-603-// send an OPTIONS response604-function sendOptionsResponse(res, options, next) {605- try {606- var body = options.join(',');607- res.set('Allow', body);608- res.send(body);609- } catch (err) {610- next(err);611- }612-}613-614-// wrap a function615-function wrap(old, fn) {616- return function proxy() {617- var args = new Array(arguments.length + 1);618-619- args[0] = old;620- for (var i = 0, len = arguments.length; i < len; i++) {621- args[i + 1] = arguments[i];622- }623-624- fn.apply(this, args);625- };626-}deleted:lib/router/layer.js-176 removed
1-/*!2- * express3- * Copyright(c) 2009-2013 TJ Holowaychuk4- * Copyright(c) 2013 Roman Shtylman5- * Copyright(c) 2014-2015 Douglas Christopher Wilson6- * MIT Licensed7- */8-9-'use strict';10-11-/**12- * Module dependencies.13- * @private14- */15-16-var pathRegexp = require('path-to-regexp');17-var debug = require('debug')('express:router:layer');18-19-/**20- * Module variables.21- * @private22- */23-24-var hasOwnProperty = Object.prototype.hasOwnProperty;25-26-/**27- * Module exports.28- * @public29- */30-31-module.exports = Layer;32-33-function Layer(path, options, fn) {34- if (!(this instanceof Layer)) {35- return new Layer(path, options, fn);36- }37-38- debug('new %s', path);39- var opts = options || {};40-41- this.handle = fn;42- this.name = fn.name || '<anonymous>';43- this.params = undefined;44- this.path = undefined;45- this.regexp = pathRegexp(path, this.keys = [], opts);46-47- if (path === '/' && opts.end === false) {48- this.regexp.fast_slash = true;49- }50-}51-52-/**53- * Handle the error for the layer.54- *55- * @param {Error} error56- * @param {Request} req57- * @param {Response} res58- * @param {function} next59- * @api private60- */61-62-Layer.prototype.handle_error = function handle_error(error, req, res, next) {63- var fn = this.handle;64-65- if (fn.length !== 4) {66- // not a standard error handler67- return next(error);68- }69-70- try {71- fn(error, req, res, next);72- } catch (err) {73- next(err);74- }75-};76-77-/**78- * Handle the request for the layer.79- *80- * @param {Request} req81- * @param {Response} res82- * @param {function} next83- * @api private84- */85-86-Layer.prototype.handle_request = function handle(req, res, next) {87- var fn = this.handle;88-89- if (fn.length > 3) {90- // not a standard request handler91- return next();92- }93-94- try {95- fn(req, res, next);96- } catch (err) {97- next(err);98- }99-};100-101-/**102- * Check if this route matches `path`, if so103- * populate `.params`.104- *105- * @param {String} path106- * @return {Boolean}107- * @api private108- */109-110-Layer.prototype.match = function match(path) {111- if (path == null) {112- // no path, nothing matches113- this.params = undefined;114- this.path = undefined;115- return false;116- }117-118- if (this.regexp.fast_slash) {119- // fast path non-ending match for / (everything matches)120- this.params = {};121- this.path = '';122- return true;123- }124-125- var m = this.regexp.exec(path);126-127- if (!m) {128- this.params = undefined;129- this.path = undefined;130- return false;131- }132-133- // store values134- this.params = {};135- this.path = m[0];136-137- var keys = this.keys;138- var params = this.params;139-140- for (var i = 1; i < m.length; i++) {141- var key = keys[i - 1];142- var prop = key.name;143- var val = decode_param(m[i]);144-145- if (val !== undefined || !(hasOwnProperty.call(params, prop))) {146- params[prop] = val;147- }148- }149-150- return true;151-};152-153-/**154- * Decode param value.155- *156- * @param {string} val157- * @return {string}158- * @private159- */160-161-function decode_param(val) {162- if (typeof val !== 'string' || val.length === 0) {163- return val;164- }165-166- try {167- return decodeURIComponent(val);168- } catch (err) {169- if (err instanceof URIError) {170- err.message = 'Failed to decode param \'' + val + '\'';171- err.status = err.statusCode = 400;172- }173-174- throw err;175- }176-}deleted:lib/router/route.js-210 removed
1-/*!2- * express3- * Copyright(c) 2009-2013 TJ Holowaychuk4- * Copyright(c) 2013 Roman Shtylman5- * Copyright(c) 2014-2015 Douglas Christopher Wilson6- * MIT Licensed7- */8-9-'use strict';10-11-/**12- * Module dependencies.13- * @private14- */15-16-var debug = require('debug')('express:router:route');17-var flatten = require('array-flatten');18-var Layer = require('./layer');19-var methods = require('methods');20-21-/**22- * Module variables.23- * @private24- */25-26-var slice = Array.prototype.slice;27-var toString = Object.prototype.toString;28-29-/**30- * Module exports.31- * @public32- */33-34-module.exports = Route;35-36-/**37- * Initialize `Route` with the given `path`,38- *39- * @param {String} path40- * @public41- */42-43-function Route(path) {44- this.path = path;45- this.stack = [];46-47- debug('new %s', path);48-49- // route handlers for various http methods50- this.methods = {};51-}52-53-/**54- * Determine if the route handles a given method.55- * @private56- */57-58-Route.prototype._handles_method = function _handles_method(method) {59- if (this.methods._all) {60- return true;61- }62-63- var name = method.toLowerCase();64-65- if (name === 'head' && !this.methods['head']) {66- name = 'get';67- }68-69- return Boolean(this.methods[name]);70-};71-72-/**73- * @return {Array} supported HTTP methods74- * @private75- */76-77-Route.prototype._options = function _options() {78- var methods = Object.keys(this.methods);79-80- // append automatic head81- if (this.methods.get && !this.methods.head) {82- methods.push('head');83- }84-85- for (var i = 0; i < methods.length; i++) {86- // make upper case87- methods[i] = methods[i].toUpperCase();88- }89-90- return methods;91-};92-93-/**94- * dispatch req, res into this route95- * @private96- */97-98-Route.prototype.dispatch = function dispatch(req, res, done) {99- var idx = 0;100- var stack = this.stack;101- if (stack.length === 0) {102- return done();103- }104-105- var method = req.method.toLowerCase();106- if (method === 'head' && !this.methods['head']) {107- method = 'get';108- }109-110- req.route = this;111-112- next();113-114- function next(err) {115- if (err && err === 'route') {116- return done();117- }118-119- var layer = stack[idx++];120- if (!layer) {121- return done(err);122- }123-124- if (layer.method && layer.method !== method) {125- return next(err);126- }127-128- if (err) {129- layer.handle_error(err, req, res, next);130- } else {131- layer.handle_request(req, res, next);132- }133- }134-};135-136-/**137- * Add a handler for all HTTP verbs to this route.138- *139- * Behaves just like middleware and can respond or call `next`140- * to continue processing.141- *142- * You can use multiple `.all` call to add multiple handlers.143- *144- * function check_something(req, res, next){145- * next();146- * };147- *148- * function validate_user(req, res, next){149- * next();150- * };151- *152- * route153- * .all(validate_user)154- * .all(check_something)155- * .get(function(req, res, next){156- * res.send('hello world');157- * });158- *159- * @param {function} handler160- * @return {Route} for chaining161- * @api public162- */163-164-Route.prototype.all = function all() {165- var handles = flatten(slice.call(arguments));166-167- for (var i = 0; i < handles.length; i++) {168- var handle = handles[i];169-170- if (typeof handle !== 'function') {171- var type = toString.call(handle);172- var msg = 'Route.all() requires callback functions but got a ' + type;173- throw new TypeError(msg);174- }175-176- var layer = Layer('/', {}, handle);177- layer.method = undefined;178-179- this.methods._all = true;180- this.stack.push(layer);181- }182-183- return this;184-};185-186-methods.forEach(function(method){187- Route.prototype[method] = function(){188- var handles = flatten(slice.call(arguments));189-190- for (var i = 0; i < handles.length; i++) {191- var handle = handles[i];192-193- if (typeof handle !== 'function') {194- var type = toString.call(handle);195- var msg = 'Route.' + method + '() requires callback functions but got a ' + type;196- throw new Error(msg);197- }198-199- debug('%s %s', method, this.path);200-201- var layer = Layer('/', {}, handle);202- layer.method = method;203-204- this.methods[method] = true;205- this.stack.push(layer);206- }207-208- return this;209- };210-});modified:lib/application.js+1 added-1 removed
12 unmodified lines13 * @private14 */15 16 var finalhandler = require('finalhandler');17-var Router = require('./router');17 var methods = require('methods');18 var debug = require('debug')('express:application');19 var View = require('./view');20 var http = require('http');2 unmodified lines23 var compileTrust = require('./utils').compileTrust;24 var flatten = require('array-flatten');25 var merge = require('utils-merge');26 var resolve = require('path').resolve;27+var Router = require('router');28 var slice = Array.prototype.slice;29 30 /**31 * Application prototype.598 unmodified linesmodified:lib/express.js+2 added-3 removed
13 unmodified lines141516171819181920212234 unmodified lines5758596062616263646537 unmodified lines13 unmodified lines
var EventEmitter = require('events').EventEmitter;var mixin = require('merge-descriptors');var proto = require('./application');var Route = require('./router/route');var Router = require('./router');var Router = require('router');var req = require('./request');var res = require('./response');
/**34 unmodified lines/** * Expose constructors. */
exports.Route = Route;exports.Route = Router.Route;exports.Router = Router;
/** * Expose middleware37 unmodified linesmodified:test/app.options.js+8 added-8 removed
10 unmodified lines11 app.put('/users', function(req, res){});12 13 request(app)14 .options('/users')15- .expect('Allow', 'GET,HEAD,PUT')16- .expect(200, 'GET,HEAD,PUT', done);15+ .expect('Allow', 'GET, HEAD, PUT')16+ .expect(200, 'GET, HEAD, PUT', done);17 })18 19 it('should only include each method once', function(done){20 var app = express();4 unmodified lines25 app.get('/users', function(req, res){});26 27 request(app)28 .options('/users')29- .expect('Allow', 'GET,HEAD,PUT')30- .expect(200, 'GET,HEAD,PUT', done);29+ .expect('Allow', 'GET, HEAD, PUT')30+ .expect(200, 'GET, HEAD, PUT', done);31 })32 33 it('should not be affected by app.all', function(done){34 var app = express();8 unmodified lines43 44 request(app)45 .options('/users')46 .expect('x-hit', '1')47- .expect('Allow', 'GET,HEAD,PUT')48- .expect(200, 'GET,HEAD,PUT', done);47+ .expect('Allow', 'GET, HEAD, PUT')48+ .expect(200, 'GET, HEAD, PUT', done);49 })50 51 it('should not respond if the path is not defined', function(done){52 var app = express();14 unmodified lines67 app.get('/other', function(req, res){});68 69 request(app)70 .options('/other')71- .expect('Allow', 'GET,HEAD')72- .expect(200, 'GET,HEAD', done);71+ .expect('Allow', 'GET, HEAD')72+ .expect(200, 'GET, HEAD', done);73 })74 75 describe('when error occurs in respone handler', function () {76 it('should pass error to callback', function (done) {39 unmodified linesmodified:test/app.router.js+1 added-1 removed
54 unmodified lines55 })56 57 it('should reject numbers for app.' + method, function(){58 var app = express();59- app[method].bind(app, '/', 3).should.throw(/Number/);59+ assert.throws(app[method].bind(app, '/', 3), /argument handler must be a function/);60 })61 });62 63 it('should re-route when method is altered', function (done) {923 unmodified linesmodified:test/app.use.js+6 added-5 removed
1 2 var after = require('after');3+var assert = require('assert');4 var express = require('..');5 var request = require('supertest');6 7 describe('app', function(){247 unmodified lines255 256 describe('.use(path, middleware)', function(){257 it('should reject missing functions', function () {258 var app = express();258- app.use.bind(app, '/').should.throw(/requires middleware function/);259+ assert.throws(app.use.bind(app, '/'), /requires middleware function/);260 })261 262 it('should reject non-functions as middleware', function () {263 var app = express();263- app.use.bind(app, '/', 'hi').should.throw(/requires middleware function.*string/);264- app.use.bind(app, '/', 5).should.throw(/requires middleware function.*number/);265- app.use.bind(app, '/', null).should.throw(/requires middleware function.*Null/);266- app.use.bind(app, '/', new Date()).should.throw(/requires middleware function.*Date/);264+ assert.throws(app.use.bind(app, '/', 'hi'), /argument handler must be a function/);265+ assert.throws(app.use.bind(app, '/', 5), /argument handler must be a function/);266+ assert.throws(app.use.bind(app, '/', null), /argument handler must be a function/);267+ assert.throws(app.use.bind(app, '/', new Date()), /argument handler must be a function/);268 })269 270 it('should strip path from req.url', function (done) {271 var app = express();257 unmodified linesmodified:test/Router.js+9 added-9 removed
25 unmodified lines26 res.end();27 });28 router.use('/foo', another);29 30- router.handle({ url: '/foo/bar', method: 'GET' }, { end: done });30+ router.handle({ url: '/foo/bar', method: 'GET' }, { end: done }, function(){});31 });32 33 it('should support dynamic routes', function(done){34 var router = new Router();4 unmodified lines39 res.end();40 });41 router.use('/:foo', another);42 43- router.handle({ url: '/test/route', method: 'GET' }, { end: done });43+ router.handle({ url: '/test/route', method: 'GET' }, { end: done }, function(){});44 });45 46 it('should handle blank URL', function(done){47 var router = new Router();16 unmodified lines64 router.get('/', function (req, res) {65 res.end();66 });67 68- router.handle({ url: '/', method: 'GET' }, { end: done });68+ router.handle({ url: '/', method: 'GET' }, { end: done }, function(){});69 });70 71 describe('.handle', function(){72 it('should dispatch', function(done){8 unmodified lines81 val.should.equal('foo');82 done();83 }84 }85- router.handle({ url: '/foo', method: 'GET' }, res);85+ router.handle({ url: '/foo', method: 'GET' }, res, function(){});86 })87 })88 89 describe('.multiple callbacks', function(){251 unmodified lines341 342 describe('.use', function() {343 it('should require arguments', function(){344 var router = new Router();345- router.use.bind(router).should.throw(/requires middleware function/)345+ assert.throws(router.use.bind(router), /argument handler is required/)346 })347 348 it('should not accept non-functions', function(){349 var router = new Router();350- router.use.bind(router, '/', 'hello').should.throw(/requires middleware function.*string/)351- router.use.bind(router, '/', 5).should.throw(/requires middleware function.*number/)352- router.use.bind(router, '/', null).should.throw(/requires middleware function.*Null/)353- router.use.bind(router, '/', new Date()).should.throw(/requires middleware function.*Date/)350+ assert.throws(router.use.bind(router, '/', 'hello'), /argument handler must be a function/)351+ assert.throws(router.use.bind(router, '/', 5), /argument handler must be a function/)352+ assert.throws(router.use.bind(router, '/', null), /argument handler must be a function/)353+ assert.throws(router.use.bind(router, '/', new Date()), /argument handler must be a function/)354 })355 356 it('should accept array of middleware', function(done){357 var count = 0;158 unmodified linesmodified:History.md+1 added
6 unmodified lines7 - `app.param(fn)`8 - `req.param()` -- use `req.params`, `req.body`, or `req.query` instead9 * change:10 - The leading `:` character in `name` for `app.param(name, fn)` is no longer removed11+ - Use `router` module for routing12 13 5.0.0-alpha.1 / 2014-11-0614 ==========================15 3010 unmodified linesmodified:package.json+1 added
45 unmodified lines46474849505152535438 unmodified lines45 unmodified lines "path-to-regexp": "0.1.6", "proxy-addr": "~1.0.8", "qs": "4.0.0", "range-parser": "~1.0.2", "router": "~1.1.2", "send": "0.13.0", "serve-static": "~1.10.0", "type-is": "~1.6.4", "vary": "~1.0.0",38 unmodified lines