Constructing regular expressions from route paths
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 9310cf9 · Browse files · 2 files·+47 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+47 added−4 removed
- tests:+1 added−1 removed
- other:+46 added−3 removed
Provenance
- Commit
3132709- Tree
a8d73fc- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
9310cf9
2 files changed+47 added−4 removed
modified:lib/express/core.js+46 added-3 removed
6 unmodified lines7891011111213141515161718192021222324252613 unmodified lines4041424343444546474858 unmodified lines10710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615716 unmodified lines6 unmodified lines
Route = Class({ init: function(method, path, fn, options){ this.method = method this.path = normalizePath(path) this.path = pathToRegexp(normalizePath(path)) this.fn = fn }, run: function(){ return process.compile('with(Express.helpers){ (' + this.fn + ')() }', this.method + '("' + this.path + '")') return process.compile('with(Express.helpers){ (' + this.fn + ')() }', this.method + '(' + this.path + ')') }})
// --- Router
Router = Class({ captures: [], route: function(request){ this.request = request return this.matchingRoute().run() },13 unmodified lines */
match: function(route) { if (this.request.method.toLowerCase() == route.method) return route.path == this.request.uri.path return (this.captures = this.request.uri.path.match(route.path)) }})
// --- Server58 unmodified lines fn = options, options = {} Express.routes.push(new Route(method, path, fn, options)) }}/** * Normalize _path_. When a RegExp it is simply returned, * otherwise a string is converted to a regular expression * surrounded by ^$. So /user/:id/edit would become: * * \^/user/(.*?)\/edit\$/ * * Each param key (:id) will be captured and placed in the * params array, so param('id') would give the string captured. * * @param {string} path * @return {regexp} * @api public */
var keys = []function pathToRegexp(path) { if (path instanceof RegExp) return path path = path.replace(/:(\w+)/g, function(_, key){ keys.push(key) return '(.*?)' }) return new RegExp('^' + escapeRegexp(path, '/ [ ]') + '$', 'i')}
/** * Escape RegExp _chars_ in _string_. Where _chars_ * defaults to regular expression special characters. * * _chars_ should be a space delimited list of characters, * for example '[ ] ( )'. * * @param {string} string * @param {string} chars * @return {Type} * @api public */
function escapeRegexp(string, chars) { var specials = (chars || '/ . * + ? | ( ) [ ] { } \\').split(' ').join('|\\') return string.replace(new RegExp('(\\' + specials + ')', 'g'), '\\$1')}
set = function(option, val) { return val == undefined ? Express.settings[option] : Express.settings[option] = val16 unmodified linesmodified:spec/spec.core.routing.js+1 added-1 removed
66 unmodified lines67 -{ get('/user') }.should.throw_error68 try { get('/user') }69 catch (e){ 70 e.stack.should.include('get')71- e.stack.should.include('"/user"')71+ e.stack.should.include('user')72 }73 end74 end75 2 unmodified lines