Q:

How do you access values passed to next() functions in Node

I'm using Node with Express.js to write a back-end for a project I'm working on.
next() functions are used in middleware to move to the next piece in the middleware chain, and finally onto the app.VERB() function. Where and how (further down the line) do I access this variable?
Example code:
app.use(function (req, res, next) {

    User.findOne({
            'email': emailValue
        }, function (err, foundUser) {

            if (err) return next(err); 

            // Else do something
        }
    }); 
});

What has access to the err value passed to next()?

A:

When you pass an error parameter into next, Express invokes whatever error middleware handler you've installed.  The error middleware function has four arguments, so you'd install your own handler as:
app.use(function(err, req, res, next) {
    // handle the err from a previous middleware's next(err) call
});

You'd typically add this at the end of your middleware chain so that it handles all other middlewares' errors.
See here for the Express documentation on this.

