Node.js连接createServer代码 [英] Node.js Connect createServer code
问题描述
我正在阅读Node.js Connect版本2.15.0:
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;
};
几个问题:
-
app.handle
似乎是原始提供的,因为有
app.handle $在proto.js中定义的c $ c>。因此,这是对闭包的使用,
和app.handle
的定义不是在节点解析函数
,但稍后在代码中?此外,
时定义的app()app
本身也在..uh中定义为
。.function app()
。该代码对我来说似乎很有趣。
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.
何时调用函数app()
?我所知道的创建服务器会创建
服务器。那么,什么时候调用该函数?我
是否会执行以下操作:
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()简单地说
utils.merge() simply says
exports.merge = function(a, b){
if (a && b) {
for (var key in b) {
a[key] = b[key];
}
}
return a;
};
这是代替继承还是一种常见的做法吗?在我看来, mixin
。
Is that a common practice to do instead of inheritance or what? It looks like mixin
to me.
推荐答案
-
app.handle
似乎是原始提供的,因为有
proto.js中定义的app.handle
。因此,这是闭包的用法,
和app.handle
的定义不是在Node解析function app()<时定义的。 / code>
,但是稍后在代码中呢?此外,app
本身也在..uh中定义为
。.function app()
。该代码对我来说似乎很有趣。
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.
是, app.handle
来自 proto
。但是不,访问 app
函数内的 app
不是闭包,在所有函数中按其名称提供函数
Yes, app.handle
comes from proto
. But no, accessing app
inside the app
function is not a closure, a function is available by its name in all function declarations.
2。什么时候调用
function app()
?我所知道的createServer
创建
服务器。那么什么时候调用该函数以及如何调用?
2. When is
function app()
invoked? All I knowcreateServer
creates the server. So when would I be invoking that function and how?
连接文档使用以下示例代码:
var app = connect();
…
http.createServer(app).listen(3000);
您可以看到 connect
(即<您发布的 createServer
函数)确实创建了 app
,并将其作为处理程序传递给实际的http服务器。 / p>
You can see that connect
(which is the createServer
function you posted) does create the app
, which is passed as a handler to the actual http server.
3。
utils.merge()
是常见的做法而不是继承吗?对我来说,这似乎是mixin
。
3. Is
utils.merge()
a common practice instead of inheritance or what? It looks likemixin
to me.
是, merge
确实将一个对象混入另一个对象。这样做是为了使用所有必需的方法扩展功能对象 app
。这是必要的,因为不可能以标准方式使用原型链定义功能。他们可能也使用了类似的方式:
Yes, merge
does mixin one object into the other. This is done to extend the function object app
with all the necessary methods. It is necessary because it is impossible to Define a function with a prototype chain in a standard way. They might have used something along these lines as well:
app.__proto__ = utils.merge(Object.create(EventEmitter.prototype), proto);
,但是 app
不再是 instanceof Function
;并且没有函数方法。
but then app
wouldn't any longer be instanceof Function
; and have no function methods.
这篇关于Node.js连接createServer代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!