I'm reading Node.js Connect version 2.15.0:
/**
* Create a new connect server.
*
* @return {Function}
* @api public
*/
function createServer() {
function app(req, res, next){ app.handle(req, res, next); }
utils.merge(app, proto);
utils.merge(app, EventEmitter.prototype);
app.route = '/';
app.stack = [];
for (var i = 0; i < arguments.length; ++i) {
app.use(arguments[i]);
}
return app;
};
A few questions:
app.handle
seems to be provided in proto, since there isapp.handle
defined in proto.js. So, this is a use of a closure, andapp.handle
is defined not at the time Node parsesfunction app()
but later on in the code? Alsoapp
itself is defined in..uh..function app()
. The code seems funny to me.When is
function app()
invoked? All I know create server creates the server. So when would I be invoking that function and how? Do I do something like:app = createServer() app.listen(3000) app(req, res, next)
utils.merge() simply says
exports.merge = function(a, b){ if (a && b) { for (var key in b) { a[key] = b[key]; } } return a; };
Is that a common practice to do instead of inheritance or what? It looks like
mixin
to me.
var app
is a result for thecreateServer()
function I posted. How doeshttp.createServer()
execute the lineapp.handle(req, res, next)
inside thecreateServer()
I posted? – Affiant