我正在尝试了解更多express 和nodejs 的内部结构。查看express的response.js
文件,它经常将几个方法分配给res
,这似乎是一个原型(prototype)。
具体来说,res
声明为 res = http.ServerResponse.prototype
。
好的,那么http
是什么? http
声明为 http = require('http')
。
因此,在express的http.js
文件中,我们看到exports = module.exports = HTTPServer;
和HTTPServer
似乎是这个方法:
function HTTPServer(middleware){
connect.HTTPServer.call(this, []);
this.init(middleware);
};
这就是我陷入困境的地方。根据我的逻辑,似乎是在 HTTPServer
方法上调用 ServerResponse
,这当然没有意义。因此,我一定错过了一些东西。
更新:
我刚刚意识到express创建了一个HTTPServer实例:
exports.createServer = function(options){
if ('object' == typeof options) {
return new HTTPSServer(options, Array.prototype.slice.call(arguments, 1));
} else {
return new HTTPServer(Array.prototype.slice.call(arguments));
}
};
所以我猜测实际上是在该实例上调用 ServerResponse
的情况?但我仍然找不到 ServerResponse
...
最佳答案
我在 express source files 中看不到任何 http.js 文件.
根据node.js documentation on http http = require('http')
将加载 http 模块,该模块具有 ServerResponse
object .
因此,表达代码通过附加方法增强了 ServerResponse 对象。
关于javascript - 深入兔子洞 - "res"在express和/或nodejs中来自哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10777153/