When working with express.js, I find it puzzling that createApplication() does not throw an error. This is because it uses app.handle(...) within an anonymous function that defines the same variable 'app'.
I attempted to replicate this in jsFiddle and encountered the expected 'app is undefined' error. The function assignment at the beginning of create Application() is what confuses me:
function createApplication() {
//New variable 'app' to be defined
//by anonymous function
var app = function(req, res, next) {
app.handle(req, res, next); // But 'app' not fully defined yet!
};
mixin(app, EventEmitter.prototype, false);
mixin(app, proto, false);
// expose the prototype that will get set on requests
app.request = Object.create(req, {
app: {\ configurable: true, enumerable: true, writable: true, value: app }
})
// expose the prototype that will get set on responses
app.response = Object.create(res, {
app: { configurable: true, enumerable: true, writable: true, value: app
}})
app.init();
return app;
}