I am currently reading this article
At the section Middleware the author is explaining very well what the benefit of next() is.
Writing this
var app = require("express")();
function checkLogin()
{
return false;
}
function logRequest()
{
console.log("New request");
}
app.get("/dashboard", function(httpRequest, httpResponse, next){
logRequest();
if(checkLogin())
{
httpResponse.send("This is the dashboard page");
}
else
{
httpResponse.send("You are not logged in!!!");
}
});
app.get("/profile", function(httpRequest, httpResponse, next){
logRequest();
if(checkLogin())
{
httpResponse.send("This is the dashboard page");
}
else
{
httpResponse.send("You are not logged in!!!");
}
});
app.listen(8080);
By using next() can be much cleaner
var app = require("express")();
function checkLogin()
{
return false;
}
function logRequest()
{
console.log("New request");
}
app.get("/*", function(httpRequest, httpResponse, next){
logRequest();
next();
})
app.get("/*", function(httpRequest, httpResponse, next){
if(checkLogin())
{
next();
}
else
{
httpResponse.send("You are not logged in!!!");
}
})
app.get("/dashboard", function(httpRequest, httpResponse, next){
httpResponse.send("This is the dashboard page");
});
app.get("/profile", function(httpRequest, httpResponse, next){
httpResponse.send("This is the dashboard page");
});
app.listen(8080);
But what I don't understand is: How does next() know if it should go as next in the /dashboard or in the /profile route handler ?