gpt4 book ai didi

javascript - 深入兔子洞 - "res"在express和/或nodejs中来自哪里?

转载 作者:行者123 更新时间:2023-12-02 19:37:32 26 4
gpt4 key购买 nike

我正在尝试了解更多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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com