Refactored Router#route()
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 7e2ccf7 · Browse files · 4 files·+5 added−31 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+5 added−31 removed
- tests:+3 added−9 removed
- other:+2 added−22 removed
Provenance
- Commit
8cac1eb- Tree
97987ce- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
7e2ccf7
4 files changed+5 added−31 removed
modified:lib/express/core.js+2 added-3 removed
129 unmodified lines130 */131 132 route: function(request){133 this.request = request134- return this.matchingRoute().run()134+ var route = this.matchingRoute()135+ return route ? route.run() : halt()136 },137 138 /**139 * Attempt to match a route from Express.routes.139- * Failure to do so will throw a NotFoundError.140 *141 * @return {Route}142 * @api private143 */1 unmodified line145 matchingRoute: function(){146 for (var i = 0, len = Express.routes.length; i < len; ++i)147 if (this.match(Express.routes[i]))148 return Express.routes[i]149- notFound()149 },150 151 /**152 * Check if _route_ matches the current request.118 unmodified linesmodified:lib/express/exceptions.js-9 removed
11 unmodified lines121314151617181920212223241617181913 unmodified lines11 unmodified lines return this.name + ': ' + this.message }})
// --- NotFoundError
exports.NotFoundError = ExpressError.extend({ name: 'NotFoundError', init: function(request) { this.message = 'failed to find ' + request.method + ' ' + jsonEncode(request.uri.path) }})
// --- InvalidStatusCode
exports.InvalidStatusCode = ExpressError.extend({ name: 'InvalidStatusCode',13 unmodified linesmodified:lib/express/helpers.js-10 removed
157 unmodified lines158 throw new InvalidStatusCode(code)159 }160 161 /**162- * Throw NotFoundError in context to the current request.163- *164- * @api public165- */166-167-exports.notFound = function() {168- throw new NotFoundError(Express.server.request)169-}170-171-/**162 * Escape special characters in _html_.163 *164 * @param {string} html165 * @return {string}85 unmodified linesmodified:spec/spec.core.routing.js+3 added-9 removed
79 unmodified lines8081828384848586875 unmodified lines93949596989910010110210310410597989910010110210310479 unmodified lines get('/foo', function(){ return 'baz' }) get('/foo').body.should.eql 'bar' get('/foo').body.should.eql 'bar' end end describe 'with no response body'5 unmodified lines end end describe 'with an unmatchable request path' it 'should throw a NotFoundError' // TODO: throw_error(NotFoundError, ...) when jspec is fixed -{ get('/something') }.should.throw_error(/NotFoundError: failed to find get "\/something"/) try { get('/something') } catch (e) { e.should.be_an_instance_of ExpressError e.should.be_an_instance_of NotFoundError } it 'should respond with 404 Not Found' get('/something').status.should.eql 404 get('/something').body.should.eql 'Not Found' end end endend