ramiel / router.js

Router.js is a simple and powerful javascript library to handle routing
116 stars 21 forks source link

Bug: Unable to call next() (next is null) #4

Closed tilleps closed 10 years ago

tilleps commented 10 years ago

The next variable for .addRoute() is null. Appears to be okay in .before()

Example code:

var router = new Router();

router.addRoute('#/user', function(req, next) {
  console.log(next);  // null
  // unable to call next( new Error('Not found'), 404);
});

router.errors(404, function(err, href) {
  console.log('404', err, href);
});

router.run('/user');

After reading the documentation, it seems that next() is only available in addRoute() if there is another matching route after it. Would be nice to be able to next() to one of the errors()

ramiel commented 10 years ago

next is populated only if there is another route which match the current url. In documentation it's suggested to check it

 if( next instanceof Function)
   next();

Anyway I understand this is not the behavior everyone want (including me) because in this way it's not possible to fire errors in a last-matching route. There is a branch called feature/next_always in which the behaviour is to have next always present. The code is already working and well documented in the branch but I'll merge it in 1.0 version because this breaks any existing code. You're are free to use that branch and to contribute to it.

Anyway I consider this bug as closed for the master branch