The code below is from connect which is a popular module in nodejs.

function createServer() {
  function app(req, res, next){ app.handle(req, res, next); }
  merge(app, proto);
  merge(app, EventEmitter.prototype);
  app.route = '/';
  app.stack = [];
  return app;
}

I'm thinking for a long time, but I don't know how the function app executes. Is anyone familiar with the code ? Please tell me how it works. Thank you!

有帮助吗?

解决方案

There is likely more to that code later on.

According to this blog,

The code above uses utils.merge to give app all the functionality of the Connect http server prototype. This includes the .use, .handle, and .listen methods.

The app also merges EventEmitter from the node.js events library. EventEmitter is where app gets app.on and app.emit from.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top