Node.js Include code createServer

I am 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.handleappears to be in proto because it exists app.handle, defined in proto.js. So this is the use of closure, and app.handleis determined not during Node parsing function app(), but later in the code? Also defined app in..uh .. itself function app(). The code seems ridiculous to me.

  • When is it called function app()? All I know is creating a server; creating a server. So, when will I refer to this function and how? I have do something like:

    app = createServer()
    app.listen(3000)
    app(req, res, next)
    
  • utils.merge () just says

    exports.merge = function(a, b){ 
      if (a && b) {
        for (var key in b) {
          a[key] = b[key];
        }   
      }
      return a;
    };
    

    Is this a common practice, not inheritance? For me it looks like mixin.

0
1
  • app.handle , app.handle, proto.js. , , app.handle Node function app() ? appin..uh.. function app(). .

, app.handle proto. , app app , .

2. function app()? , , createServer    . , ?

connect docs :

var app = connect();
http.createServer(app).listen(3000);

, connect ( createServer, ) app, http-.

3. utils.merge() ? mixin.

, merge . app . , . - :

app.__proto__ = utils.merge(Object.create(EventEmitter.prototype), proto);

app instanceof Function; .

0

All Articles