2

The Express documentation states the following about next('route') callback:

You can provide multiple callbacks, and all are treated equally, and behave just like middleware, except that these callbacks may invoke next('route') to bypass the remaining route callback(s). You can use this mechanism to perform pre-conditions on a route then pass control to subsequent routes when there is no reason to proceed with the route matched.

and

To skip the rest of the middleware functions from a router middleware stack, call next('route') to pass control to the next route. NOTE: next('route') will work only in middleware functions that were loaded by using the app.METHOD() or router.METHOD() functions.

What is the response to the client when a middleware calls next('route') and there is no other matching route?

micnil
  • 4,705
  • 2
  • 28
  • 39

2 Answers2

1

It will exit the Router and continue on with the rest of your application middleware and if still nothing ends in a response then a 404 error will be thrown and will be handled by the error handlers.

roflmyeggo
  • 1,724
  • 16
  • 18
  • ah, okey. What if I have not specified an error handler, what does the default error handler respond with? – micnil Apr 10 '16 at 21:51
  • Since you tagged this question with `express` I included the last bit because by default the error handling middleware is included after the `Router` middleware. Essentially it will just leave the router and move onto the next application middleware (this is what you are seeing with `app.use`), whatever it happens to be. – roflmyeggo Apr 10 '16 at 21:57
1

The response to the client is a status 404 error. This is not caught by the routers error handlers.

If you want to catch the 404 error for some reason, see this question.

However beware of matching routes:

router.get('/profile', auth.hasRole('User'), controller.showProfile);
router.get('/:id', controller.show);

I had the hasRole('User') function call next('route') if the client did not have that role. Controll was then passed to the '/:id' route as this matched the request uri, causing errors.

Community
  • 1
  • 1
micnil
  • 4,705
  • 2
  • 28
  • 39
  • It is only a 404 if you have the error handlers in your application that will catch the router that did not return a response. You can test this by adding some middleware right after your routers that returns a specific error code of your choice and some HTML to verify this. This is just the way that ExpressJS projects are setup by default (the error handling middleware is right after the routers in your application). I have specififed in my answer to say it is the application error handlers, not the router (there are none unless you provide them). – roflmyeggo Apr 11 '16 at 00:42
  • No it is a 404 either way. I am saying that you cannot provide an error handler (a function with 4 parameters: err, req, res, next) anywhere in the application stack that will catch the error of calling next('route') when there is no other matching route. And I believe this is correct. But I guess you mean that I can add a route handler (a function with 3 parameters: req, res, next) in the end of my router stack that responds with an error. Then this is only called if none of the other route handlers responded to the request. – micnil Apr 11 '16 at 01:01
  • Good point, yes I was referring to creating your own 'error handler' right after the routes and crafting your response from there. Good answer. Basically comes down to all middleware will be tried and if nothing returns a response then a 404 error will be thrown. I have updated my answer to be more explicit and concise. – roflmyeggo Apr 11 '16 at 03:37