Suppose you have a simple block of code like this:
app.get("https://stackoverflow.com/", function(req, res){
res.send('Hello World');
});
This function has two parameters, req
and res
, which represent the request and response objects respectively.
On the other hand, there are other functions with a third parameter called next
. For example, lets have a look at the following code:
app.get('/users/:id?', function(req, res, next){ // Why do we need next?
var id = req.params.id;
if (id) {
// do something
} else {
next(); // What is this doing?
}
});
I can’t understand what the point of next()
is or why its being used. In that example, if id doesn’t exist, what is next
actually doing?