Execution order of callbacks in Express Post function Execution order of callbacks in Express Post function express express

Execution order of callbacks in Express Post function


Doesn't matter what the callbacks return in this case. They are invoking the next function (the third parameter that is not defined in this specific passport example). When next function is called then the next middleware in sequence is invoked with the same parameters as the first (same req, same res)

Say you have this code

function myFirstMiddleware(req, res, next) {//note the third parameter    //you can change req and res objects    next();}function secondMiddlewareInTheSequence(req, res, next) {    //here you get the changes you've done to req and res since they are    //the same objects    res.end();}app.post('/', myFirstMiddleware, secondMiddlewareInTheSequence);

What passport.authenticate does internally is to call next if your request is properly authenticated, so your middleware is passed control.

You may even want not to pass the control to further middlewares, based on a given condition (pretty much what passport does), like this.

function checkSecret(req, res, next) {    if(req.query && req.query.secret === '42') {        console.log('you are allowed to pass!');        next();    } else {        console.log('you shall not pass!');        res.status(403).end();    }}

The above function if placed before any other middleware would prevent requests not containing the secret to continue!