routes.js icon indicating copy to clipboard operation
routes.js copied to clipboard

Adding routes with the same path causes problems

Open glassresistor opened this issue 11 years ago • 22 comments
trafficstars

In my code im doing something about the same as this

router.addRoute('samePath', function1);
rouder.addRoute('samePath', function2);

I expected to be able to use match continuation so that both functions ran like function1 function2. It looks like the same your handling the routeMap as a associated array https://github.com/aaronblohowiak/routes.js/blob/master/index.js#L135 means that if something is added with the same path the 2nd one will override the first and also be run twice. I don't think this is the desired(by you) behaviour. I believe the easiest solution is to add the router to a list like [[path, function], [path, function]] and use the numeric index to look them back up.

glassresistor avatar Oct 27 '14 23:10 glassresistor

I'm not going to change the internals.

I recommend you do

router.addRoute('samePath', function () {
  // some magic.
  function1();
  // some more custom magic.
  function2();
});

Raynos avatar Oct 27 '14 23:10 Raynos

Just pointing out that doing routeMap the way you are means that you can not use the same path twice and that I didn't expect that to be a behaviour you wanted specifically that it just overrites the first route. Meaning it has bad undocumented behaviour, if you don't want to change it atleast document the bug. I'll be forking and fixing this myself but don't plan on using a continuing to use routes as is.

glassresistor avatar Oct 27 '14 23:10 glassresistor

Here it is working and with tests https://github.com/motherjones/routes.js

glassresistor avatar Oct 29 '14 23:10 glassresistor

Cool, nice work.

Raynos avatar Oct 29 '14 23:10 Raynos

actually hit a weird issue with fn being dropped but the tests pass. ill message when i figure it out.

On Wed, Oct 29, 2014 at 4:21 PM, Raynos (Jake Verbaten) < [email protected]> wrote:

Cool, nice work.

— Reply to this email directly or view it on GitHub https://github.com/aaronblohowiak/routes.js/issues/31#issuecomment-61022255 .

Mikela

glassresistor avatar Oct 29 '14 23:10 glassresistor

nvm it was just an issue with the dist folder it works great even in my project

On Wed, Oct 29, 2014 at 4:27 PM, Mikela Clemmons [email protected] wrote:

actually hit a weird issue with fn being dropped but the tests pass. ill message when i figure it out.

On Wed, Oct 29, 2014 at 4:21 PM, Raynos (Jake Verbaten) < [email protected]> wrote:

Cool, nice work.

— Reply to this email directly or view it on GitHub https://github.com/aaronblohowiak/routes.js/issues/31#issuecomment-61022255 .

Mikela

Mikela

glassresistor avatar Oct 29 '14 23:10 glassresistor

First of all: Thx for the cool module. :+1:

I've expected the behavior to be like @glassresistor described it (and I think that's the way how the connect router works). It imho defeats the purpose of a middleware-architecture where you compose small functions to a big application.

I'd be glad to see this feature in the "official" module.

jhnns avatar Nov 07 '14 15:11 jhnns

Independent of that feature: The current behavior definitely seems like a bug, because when I add three different functions for the same name the last functions gets called three times. I can't imagine a situation where this is useful.

jhnns avatar Nov 07 '14 15:11 jhnns

@jhnns thats for the +1 the authors suggestion is great so I'll be taking his advice, go ahead and use my fork its totally stable and in use in a project of mine I'll be republishing this with a fix for afew other issues like match not having the Router properly bound to it so match can be used to pass down context and some cleanup. Any help naming it would be loved. So far its just moreRoutes

glassresistor avatar Nov 07 '14 18:11 glassresistor

Just blew my morning on this issue. It would be helpful if there was a warning visible somewhere in the documentation, or a change to fix the underlying probelm.

greim avatar Nov 07 '14 23:11 greim

I'd still like to see this feature in the original module because imho that's not a worthy reason to fork the module (especially if more people expected it the other way). But if that's @Raynos last word I'd name it something like signal-box or something (still available on npm :grinning:)

Or routes2 (like eventemitter2) to refer to the original api.

jhnns avatar Nov 09 '14 18:11 jhnns

Here is the new library on github https://www.npmjs.org/package/i40 and npm https://www.npmjs.org/package/i40

I'll update credits and says its a fork asap. @Raynos if you could link to it with bug that would be cool. I can't find the thread where this was requested.

glassresistor avatar Nov 10 '14 19:11 glassresistor

added PR to link to new library here https://github.com/aaronblohowiak/routes.js/pull/35

glassresistor avatar Nov 10 '14 20:11 glassresistor

Released v2.0.0

If you define path twice it now throws an exception.

Raynos avatar Nov 10 '14 21:11 Raynos

@jhnns @greim please try the https://www.npmjs.org/package/i40 package and let me know if there is any issues, i'll not change anything except bugs on version 1.0.X but 1.1.X will probably add some features and change enough code something might change. Good luck sorry you ran into the same issue as me. I wasted a decent amount of time thinking it was me.

glassresistor avatar Nov 10 '14 21:11 glassresistor

Cool, I'll try that. Thx @glassresistor :+1:

jhnns avatar Nov 10 '14 21:11 jhnns

I've updated i40 to version 1.3.0 and removed some of the awkward stuff and updated docs https://www.npmjs.com/package/i40 I'm going to eventually release a 2.0 which will probably not offer breaking changes but will move to using prototypes properly, use mocha tests, docs and add afew things like addRoutes and named route lookup similar to what django offers maybe even optional url helpers from an inner require.

cc: @jhnns

glassresistor avatar Dec 11 '14 01:12 glassresistor

Nice, thx for the update!

jhnns avatar Dec 11 '14 09:12 jhnns

@glassresistor if you're maintaining a fork; mind enabling issues for i40 repo?

dashed avatar Dec 15 '14 17:12 dashed

@Dashed ok I added an issues and changed the name to i40 from routes https://github.com/glassresistor/i40

glassresistor avatar Dec 15 '14 20:12 glassresistor

At this point, this module is no longer maintained.

I use:

  • https://github.com/Matt-Esch/http-hash

and @glassresistor maintains

  • https://github.com/glassresistor/i40

I should update the README.

Raynos avatar Dec 15 '14 21:12 Raynos

I updated the README with https://github.com/aaronblohowiak/routes.js#alternative-routers

Raynos avatar Dec 15 '14 21:12 Raynos