Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between next() and next('route') in an expressjs app.VERB call?

The docs read:

The app.VERB() methods provide the routing functionality in Express, where VERB is one of the HTTP verbs, such as app.post(). Multiple callbacks may be give, all are treated equally, and behave just like middleware, with the one exception that these callbacks may invoke next('route') to bypass the remaining route callback(s). This mechanism can be used to perform pre-conditions on a route then pass control to subsequent routes when there is no reason to proceed with the route matched.

What do they mean by "bypass the remaining route callbacks?"? I know that next() will pass control to the next matching route. But... what function will get control with next('route')...?

like image 739
Merc Avatar asked Aug 04 '12 02:08

Merc


People also ask

What does next () do in Express?

The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware. Middleware functions can perform the following tasks: Execute any code.

What does next () do in Nodejs?

next() : It will run or execute the code after all the middleware function is finished. return next() : By using return next it will jump out the callback immediately and the code below return next() will be unreachable.

What is Express Router ()?

The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests.

What is next callback function?

callback: It is the callback function that contains the request object, response object, and next() function to call the next middleware function if the response of the current middleware is not terminated. In the second parameter, we can also pass the function name of the middleware.


2 Answers

I hated it when I answer my own question 5 minutes later. next('route') is when using route middleware. So if you have:

app.get('/forum/:fid', middleware1, middleware2, function(){   // ... }) 

the function middleware1() has a chance to call next() to pass control to middleware2, or next('route') to pass control to the next matching route altogether.

like image 128
Merc Avatar answered Oct 11 '22 14:10

Merc


The given answer explains the main gist of it. Sadly, it is much less intuitive than you might think, with a lot of special cases when it is used in combination with parameters. Just check out some of the test cases in the app.param test file. Just to raise two examples:

  1. app .param(name, fn) should defer all the param routes implies that if next("route") is called from a param handler, it would skip all following routes that refer to that param handler's own parameter name. In that test case, it skips all routes refering to the id parameter.
  2. app .param(name, fn) should call when values differ when using "next" suggests that the previous rule has yet another exception: don't skip if the value of the parameter changes between routes.

...and there is more...

I'm sure there is a use-case for this kind of next('route') lever, but, I agree with previous comments in that it certainly makes things complicated and non-intuitive.

like image 30
Domi Avatar answered Oct 11 '22 12:10

Domi