0

There's a line of code using next(); in this Express app (for Nodejs) that I don't understand. I wonder if you could clarify.

In index.js, the express app calls a function isLoggedInMiddleware. It doesn't pass any parameters

 app.use(sessionHandler.isLoggedInMiddleware);

Here is that function. When it was called, it wasn't passed any parameters, but it's set up to accept three, with next being the last, which is called as the return value of getUsername.

this.isLoggedInMiddleware = function(req, res, next) {
    var session_id = req.cookies.session;
    sessions.getUsername(session_id, function(err, username) {
        "use strict";

        if (!err && username) {
            req.username = username;
        }
        return next();
    });
}

This is getUserName which next(); gets passed to as part of the callbak. Can you explain how next(); was being used? what is it in this context? what is it doing?

this.getUsername = function(session_id, callback) {
        "use strict";

        if (!session_id) {
            callback(Error("Session not set"), null);
            return;
        }

        sessions.findOne({ '_id' : session_id }, function(err, session) {
            "use strict";

            if (err) return callback(err, null);

            if (!session) {
                callback(new Error("Session: " + session + " does not exist"), null);
                return;
            }

            callback(null, session.username);
        });
    }
BrainLikeADullPencil
  • 11,313
  • 24
  • 78
  • 134

4 Answers4

1

It passes the control back to the express app to invoke next middleware/request handler in chain.

Here:

app.use(sessionHandler.isLoggedInMiddleware);

you told express to use isLoggedInMiddleware as middleware.

Every middleware receives:

  • a request - to enrich and/or query,
  • a response - in case they want to write something (ie. an error),
  • a callback that allows express to continue processing the request.

That middleware, when called by express, enriches the request object with user data and then returns the control back to express, which then continues to next middleware or to handler.

soulcheck
  • 36,297
  • 6
  • 91
  • 90
1

From what I understand, it passes control to the next route handler to handle the request. Read up on Route Middleware.

federico-t
  • 12,014
  • 19
  • 67
  • 111
0

You're confusing passing a function with calling it.

app.use(sessionHandler.isLoggedInMiddleware);

This is not isLoggedInMiddleware being called; it's only being passed. app.use takes a parameter that should be a function; during the request, it will call that function with the appropriate parameters (the request object, the response object, and a callback).

Similarly,

sessions.getUsername(session_id, function(err, username) {
    "use strict";

    if (!err && username) {
        req.username = username;
    }
    return next();
});

The return value of next is not being passed to getUsername; that entire function is being passed. By calling next, it passes control back to the express framework (saying, essentially, "I'm done; call the next middleware").

Aaron Dufour
  • 17,288
  • 1
  • 47
  • 69
0

I think that is clear that the calling of next means "please, go to the next middleware in the list". However, what is more interesting is why there is a return before it.

Krasimir
  • 13,306
  • 3
  • 40
  • 55