Make route params request specific
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 37948d8 · Browse files · 3 files·+30 added−30 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+30 added−30 removed
- other:+30 added−30 removed
Provenance
- Commit
d0dae12- Tree
5e559b8- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
37948d8
3 files changed+30 added−30 removed
modified:examples/app.js+22 added-10 removed
2 unmodified lines34567789101112131415161617181920212215 unmodified lines38394041424344454640414243444546474849505152535455485657585960616263646566672 unmodified linesrequire('express')require('express/plugins')
configure(function(){ use(CommonLogger) use(Profiler) use(MethodOverride) use(ContentLength) set('root', dirname(__filename)) enable('helpful 404') enable('show exceptions') enable('cache views')})
var messages = []var messages = [], path = require('path'), posix = require('posix')
get('/', function(){ redirect('/chat')})15 unmodified lines contentType('json') return JSON.encode(messages)})
StaticFile = Class({ init: function(path) { this.path = path }, get('/public/:file', function(file){ var path = dirname(__filename) + '/public/' + file require('path').exists(path, function(exists){ if (!exists) halt() require('posix').cat(path).addCallback(function(content){ contentType(path) halt(200, content) send: function() { var self = this path.exists(this.path, function(exists){ if (!exists) halt() posix.cat(self.path).addCallback(function(content){ contentType(self.path) halt(200, content) }) }) }) }})
get('/public/:file', function(file){ (new StaticFile(dirname(__filename) + '/public/' + file)).send()})
get('/error', function(){ throw new Error('oh noes!')})
run()modified:lib/express/core.js+7 added-19 removed
110 unmodified lines111 112 Router = Class({113 114 /**115- * Parameter hash.116- */117- 118- params: {},119- 120- /**121- * Route match captures array.122- */123- 124- captures: [],125- 126- /**115 * Route the given _request_, returning116 * the value returned by Route#run()117 *118 * @param {object} request4 unmodified lines123 route: function(request){124 this.request = request125 var route = this.matchingRoute()126 if (route)139- return route.run(this.captures.slice(1))127+ return route.run(this.request.captures.slice(1))128 else if (accepts('html') && set('helpful 404'))129 halt(404, require('express/pages/not-found').render())130 else131 halt()23 unmodified lines155 */156 157 match: function(route) {158 if (this.request.method.toLowerCase() == route.method)171- if (this.captures = this.request.uri.path.match(route.path)) {159+ if (this.request.captures = this.request.uri.path.match(route.path)) {160 this.mapParams(route)161 return true162 }163 },164 165 /**178- * Map #captures to #params based on the166+ * Map #request.captures to #request.params based on the167 * given _route_ keys.168 *169 * @param {Route} route170 * @api private1 unmodified line172 173 mapParams: function(route) {174 var self = this175 $(route.keys).each(function(key, i){188- self.params[key] = self.captures[++i]176+ self.request.params[key] = self.request.captures[++i]177 })178 }179 })180 55 unmodified lines236 */237 238 route: function(request, response){239 var body 252- response.headers = {}240+ response.headers = {}, request.params = {}241 this.request = request, this.response = response242 this.request.uri.path = normalizePath(request.uri.path)243 this.request.uri.params = parseNestedParams(request.uri.params)244 this.request.uri.post = 1 unmodified line246 parseParams(request.body) :247 {}248 trigger('request')249 try {262- if (typeof (body = Express.router.route(request)) == 'string')250+ if (typeof (body = (new Express.router).route(request)) == 'string')251 halt(200, body)252 }253 catch (e) {254 if (e instanceof ExpressError)32 unmodified lines287 routes: [],288 plugins: [],289 settings: {},290 server: new Server,303- router: new Router291+ router: Router292 }293 294 // --- Defaults295 3 unmodified linesmodified:lib/express/helpers.js+1 added-1 removed
96 unmodified lines97 * @api public98 */99 100 exports.param = function(key) {101- return Express.router.params[key] ||101+ return Express.server.request.params[key] ||102 Express.server.request.uri.post[key] ||103 Express.server.request.uri.params[key]104 }105 180 unmodified lines