0

I am developing apis with nodejs. I have seen there is a parameter passed in the function with req, res like this:

bookRouter.use('/:bookId', function(req,res,next){
        Book.findById(req.params.bookId, function(err,book){
            if(err)
                res.status(500).send(err);
            else if(book)
            {
                req.book = book;
                next();
            }
            else
            {
                res.status(404).send('no book found');
            }
        });
    });

I never used this function in my development and I want to know if i really need to use this?

  • This is how the express framework work (incidentally, the req,res is how the built-in http library also work - express make their API compatible with the http library). If you don't want to use express then you don't need it. If you don't want to create a website then you don't even need the req and res thing. – slebetman Aug 30 '16 at 07:12
  • Also related: http://stackoverflow.com/questions/13133071/express-next-function-what-is-it-really-for – Spencer Wieczorek Aug 30 '16 at 07:14

5 Answers5

2

From documentation:

You can provide multiple callback functions that behave like middleware to handle a request. The only exception is that these callbacks might invoke next('route') to bypass the remaining route callbacks. You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there’s no reason to proceed with the current route.

A single callback function can handle a route. For example:

app.get('/example/a', function (req, res) {
  res.send('Hello from A!');
});

More than one callback function can handle a route (make sure you specify the next object). For example:

app.get('/example/b', function (req, res, next) {
  console.log('the response will be sent by the next function ...');
  next();
}, function (req, res) {
  res.send('Hello from B!');
});
Istiak Morsalin
  • 10,621
  • 9
  • 33
  • 65
  • When I first saw this, the question that came to mind was "Why would one want multiple callback functions for a single route/endpoint?" ... But since then I've thought about it ... Maybe you want to run the same function at the start of processing all routes, and THEN handle each route individually. So 3 routes will have the callbacks [common_cb, route1_cb], [common_cb, route2_cb], and [common_cb, route3_cb]. But then I thought... This is really no different than middleware, right? And after thinking some more I concluded that it's just two different ways to control what your sever does. – Ari Sweedler Dec 19 '18 at 04:22
0

I'm not that strong in node.js but the aim is to call a function when the book with the given ID is found. The function to be called is passed in as next. There may be other middleware components involved, so calling next() makes sure that upon success of this part, the next middleware can process the request.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

The next function will call the next middleware function in the stack. See https://expressjs.com/en/guide/using-middleware.html for more details.

Hope this helps.

Vincent Schöttke
  • 4,416
  • 2
  • 12
  • 12
0

You don't have to use next() because your action is the last action which processes request. Here's documentation fragment:

Middleware functions are functions that have access to the request object (req), the response object (res), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next.

Middleware functions can perform the following tasks:

  1. Execute any code.
  2. Make changes to the request and the response objects.
  3. End the request-response cycle.
  4. Call the next middleware function in the stack.

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.

Community
  • 1
  • 1
Fka
  • 6,044
  • 5
  • 42
  • 60
0

Node is event based language with the callback. If your event triggered successfully and you want to execute the some function then it will be passed as an argument to this. But it does not create problem if you don't pass function or you don't use it.

Vora Ankit
  • 682
  • 5
  • 8