In many examples of Nodejs/Express
, I see that calling next()
is optional in case of success.
exports.postLogin = (req, res, next) => {
passport.authenticate("local", (err, user, info) => {
if (err) {
return next(err);
}
req.logIn(user, err => {
if (err) {
return next(err);
}
req.flash("success", { msg: "Success! You are logged in." });
res.redirect(req.session.returnTo || "/");
});
})(req, res, next);
};
Also, it is easy to skip callback next
in the args:
exports.postLogin = (req, res) => {
res.render('some-template', locals);
}
If I compare this with async
lib or typical Javascript's asynchronous model, a missing callback will not give data or halt the process.
What does express
do to take care of control flow when next
is not called?
express
code, theres.end()
is actually a http.ServerResponse.end. All other methods like res.render(), sendFile(), send() internally calls res.end(), the true callback! – Mireillemireles