admin管理员组

文章数量:1277258

I am new to NodeJs ing 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. /
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 remended 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 es 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?

I am new to NodeJs ing 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./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 remended 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 es 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?

Share Improve this question asked Nov 4, 2018 at 4:50 Dev1ceDev1ce 5,95424 gold badges98 silver badges160 bronze badges 1
  • 2 You'd be surprised how mon 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 Commented Nov 4, 2018 at 5:07
Add a ment  | 

3 Answers 3

Reset to default 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 ing 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./en/guide/using-middleware.html

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./en/guide/using-middleware.html

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

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./en/guide/writing-middleware.html

本文标签: javascriptWhat is next() in NodeJsStack Overflow