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

标签 javascript node.js express

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

相关文章:

javascript - 在 JavaScript 中,第一个 bind 确定 "this"是谁? (绑定(bind)两次或第二次都没关系)

javascript - firewatch 视差效果与上面的另一个 div 和内容

javascript - Materialise Sidenav 不适用于 iOS 或 iPhone,但适用于所有其他设备

node.js - 如何将 ssl private-key.txt 转换为 private.key 扩展名

database - Oracle 创建数据库链接

css - 让我的 CSS 模块在我的 Express 应用程序上运行

templates - Jade - 从不同目录加载模板

javascript - mootools 和 nodejs

javascript - 缓冲区读写 float

node.js - Cassandra 写入性能