baseprime / grapnel

The smallest JavaScript router with named parameters, HTML5 pushState, and middleware support
http://grapnel.js.org
468 stars 40 forks source link

is there a way to set priority? #22

Closed gaboesquivel closed 9 years ago

gaboesquivel commented 9 years ago

I would like to set priority for conflicting routes like these

    router.get('/doSomethingElse', doSomethingElse);

    //displays the JST that matches the pagename in the url
    router.get('/:page?', function (req) {
      var p = req.params.page || 'homepage';
      document.getElementByID('main').innerHTML = JST[p](getData(p));
    });

problem is doSomethingElse is never called. so I have to check params inside my other route. It would be nice to be able specify route priority.

baseprime commented 9 years ago

The route priority is based on when the route is created ascending from sooner to later. For example, if you had your first route after your second route, it will have a lower priority.

// First priority
router.get('/:page?', handler);

// Third priority
setTimeout(function(){
    router.get('/async/:page?', handler);
}, 2000);

// Second priority
router.get('/doSomethingElse', doSomethingElse);