mjackson / strata

A modular, streaming HTTP server for node.js
http://stratajs.org
366 stars 35 forks source link

regex route fails sporadically #46

Closed nicholascloud closed 11 years ago

nicholascloud commented 11 years ago

I have defined the following route: /^\/.*$/ig which should capture any URL. It is a catch-all route that is designed to serve up my SPA after static URLs (e.g., public files or the /login route) have been evaluated.

When I run my app, the route will initially be hit correctly, but upon refresh I will get a 404. If I refresh again, I will see the page; if I refresh after that, a 404, and so on.

What happens is that the regex match object in the routes module is null every other request. See this screenshot for an example. Notice that even though match is null, my watch evaluates correctly.

I believe this StackOverflow post describes the cause of this problem: RegExp.exec() returns NULL sporadically

This might be mitigated by setting [regex].lastIndex = 0 after every iteration of the loop.

nicholascloud commented 11 years ago

I have confirmed that this, in fact, solves my problem.

var match = pattern.exec(env.pathInfo);
pattern.lastIndex = 0;

Does anyone know if this would introduce other possible issues? I'm not an expert on JS regex objects :(

nicholascloud commented 11 years ago

I added a unit test and determined that this only occurs because of my g flag in the regex. If g is omitted, the test passes, but if it is added, the test fails, and also causes one other test to fail.

Should the g flag be supported in regex routes?

nicholascloud commented 11 years ago

I am closing this issue because I can think of no good reason for having a g flag in a route. I'll remove mine, it fixes the problem.