当我阅读 npm 包发送时,这.res 从何而来

  • 本文关键字:res npm 包发送 node.js express
  • 更新时间 :
  • 英文 :


我是nodejs的新手,正在学习表达的工作原理。我在 npm 上发现了一个包调用send。所以我阅读了代码,但我有一个问题this.res从何而来。

SendStream.prototype.isCachable = function isCachable () {
    var statusCode = this.res.statusCode
    return (statusCode >= 200 && statusCode < 300) || statusCode === 304
}

我读取了发送模块继承的流的构造函数。我在该构造函数中找不到this.res。如果你能给出一些想法来找。这将有很大帮助。

在我看来

,只有在代码中调用.pipe()时才设置this.res

SendStream.prototype.pipe = function pipe (res) {
  // root path
  var root = this._root
  // references
  this.res = res;                    // <=========== here's where it is set
  // decode the path
  var path = decode(this.path)
  if (path === -1) {
    this.error(400)
    return res
  }
  ....
}

而且,由于此库的目的似乎是将数据流式传输到 http 响应,因此您使用此库的方式似乎始终是 send(...).pipe(res) .

最新更新