flatiron/director — Issue #51: Problem with regex matching "\/"
Author: phzbox
Created: 2011-11-27 17:46:28+00:00
State: open

Code example:

----------------------------------------------------------------------------
router.get(/sup\/meh/, function () {
this.res.writeHead(200, { 'Content-Type': 'text/plain' })
this.res.end('hello world\n');
});
----------------------------------------------------------------------------

If I try to access: /sup/meh, I get this error:
----------------------------------------------------------------------------
/usr/local/lib/node_modules/director/lib/director/router.js:420
match = path.match(new RegExp('^' + exact));
^
SyntaxError: Invalid regular expression: /^/sup\/: \ at end of pattern
at new RegExp (unknown source)
at [object Object].traverse (/usr/local/lib/node_modules/director/lib/director/router.js:420:26)

----------------------------------------------------------------------------

I've debugged a little bit and came to the conclusion that the problem is:
----------------------------------------------------------------------------
if (path.source) { // ~ line 200
path = path.source
}
----------------------------------------------------------------------------

The problem is when we split based on the '/' delimiter, the parts become:
'sup\\', 'meh'.. and when later on it tries to match 'sup\\' it obviously says there's a regex error.

My current quick fix: (Replacing \/ by /)
if (path.source) {
path = path.source.replace('\\/', '/');
}

Hope it helps