6

I am new to NodeJs coming from Java background, I'm trying to understand the use of the convention next() call.

I've gone through the following posts -
1. javascript node.js next()
2. https://www.naeemrana.com/node-js/express-js-middleware-what-is-next/
3. When to use next() and return next() in Node.js

What I could make of them is next() is similar to a callback or maybe a reference of the next function to be executed after the current function's execution,
Also understood it's recommended to use return next(); instead of next(); to avoid the function to be re-executed.

Following is a sample code from one the links -

app.get('/users/:id', function(req, res) {
    var user_id = req.params.id;

    if(user_id) {
      // do something
    } else {
      next(); // here comes middleware.
    }
});

What I don't understand here is the function being executed does not have the 3rd argument,
but it does invoke the next() function, what is the idea here?
Is the use of this convention only to be used on routes logic?
Is next() a callback? is it a reference to the next function to be executed? or something else?

Dev1ce
  • 5,390
  • 17
  • 90
  • 150
  • 2
    You'd be surprised how common it is for articles to provide incorrect code. One of the articles you provided forgot to include `next` in the function parameters. Your presumptions are correct, next is a callback function, a reference to follow-up code. – Vic Nov 04 '18 at 05:07

3 Answers3

5

Node.Js doesn't know next. It's a pattern used by the express framework.

As express has a concept of middlewares that can be called in each and every request coming into it.

If the current middleware function does not end the request-response cycle, it must call next() to pass control to the next middleware function. Otherwise, the request will be left hanging.

var app = express()

app.use(function (req, res, next) {
  if(condition_to_return_from_here){
    res.end(req.params.id)
  }else{
  console.log('Time:', Date.now())
  next()
 }
})

More on expressjs and middleware - https://expressjs.com/en/guide/using-middleware.html

Dale K
  • 25,246
  • 15
  • 42
  • 71
front_end_dev
  • 1,998
  • 1
  • 9
  • 14
3

next() is not a nodejs thing but a express middleware function. Calling next() basically moves to the next middleware function. express is a web application for node.

You can read more https://expressjs.com/en/guide/using-middleware.html

Also, read through Express next function, what is it really for? which should you information.

Hozefa
  • 1,676
  • 6
  • 22
  • 34
0

next() is actually callback argument to the middleware function, called "next" by convention. When you use next parameter(as a parameter on middleware function) and then call next(), program moves to the next middleware function.

However express is saying "Notice the call above to next(). Calling this function invokes the next middleware function in the app. The next() function is not a part of the Node.js or Express API, but is the third argument that is passed to the middleware function."

So, I understand next() is not Nodejs thing. But also, next() is not express thing, I think.

you can read more: https://expressjs.com/en/guide/writing-middleware.html

Alper Guven
  • 81
  • 1
  • 5