Jay,
I think I have now understood enough, if not to answer your question, at least to help us ask better questions (thanks to Caridy, Ronald, and everybody here -- although not completely unblocked, I am making progress).
First off, a couple general observations.
If you require something and it is not found, make sure the module you are requiring is in your NODE_PATH. That much we've figured out in the top part of this thread.
Since the magic of middleware insertion has not been properly explained, it may be helpful to understand that module.exports in each of the middleware files listed in application.json defines the function that will be called during each request. For each middleware, this request callback function will have this signature:
module.exports = function (request, response, next) {
...
next();
};
Here you can see how different layers of middleware are chained. If you want to break the chain, just don't call the next middleware in the stack. Their stacking order is determined by the order they appear in application.json
Now, if you want to do any initialisation or set context for a middleware request callback just once, you can do it in each middleware file outside the callback function that gets assigned to module.exports. Anything you write there will be executed when mojito builds its middleware chain on start-up.
That's how much I've understood so far. I believe you don't want to set up your own express because express is already part of mojito and you can access it through the request object in your middleware callback that runs once for each request. Setting up a new instance of express would make no sense. Same applies to connect. But passport is not there, so you do need to set it up during start-up the way Rodrigo has shown above:
var passport = require('passport');
module.exports = passport.initialize();
The magic not visible here is that the call to passport.initialize() returns a properly formed middleware callback. So, if you want to further modify your passport by, for example, adding a strategy, you do:
var passport = require('passport');
var LocalStrategy = require("passport-local").Strategy;
passport.use(new LocalStrategy(
function(username, password, done) {
...
}
));
module.exports = passport.initialize();
What I still do not understand is how does one go about augmenting the already instantiated objects like express and connect outside the middleware callbacks, i.e., at the start-up stage (if one needs to).
I understood Caridy as saying that you can apply all required customisations on the app object returned by Mojito.createServer() in server.js, instead of running mojito start, in which case the reference to the app object never gets exposed outside the middleware callbacks.
The call to createServer() returns a fully configured app object with middleware set up and chained in. I haven't explored this option yet because it did not work in the previous version of mojito I had installed. I just verified that it works now (mojito@0.5.5).