简体   繁体   English

response.write - 它是从可写流还是从http.ServerResponse调用write?

[英]response.write - does it call write from writable stream or from http.ServerResponse?

I am new to node. 我是节点新手。 In a setup like this 在这样的设置中

server.on('request', function(request, response) {
  // the same kind of magic happens here!
});

I presume response is an instance of http.ServerResponse . 我认为responsehttp.ServerResponse一个实例。 But one tutorial I read says: http.ServerResponse is WritableStream . 但我读过的一个教程说: http.ServerResponseWritableStream

Now both writable stream and http.ServerResponse have write methods; 现在可写流和http.ServerResponse都有写方法; here and here . 在这里这里

My question is when I call response.write which version of write gets called? 我的问题是当我调用response.write调用哪个版本的write时? One from http.ServerResponse or one from writable stream? 一个来自http.ServerResponse或一个来自可写流?

http.ServerResponse implements a version of the stream.Writable interface but does not inherit its actual implementation. http.ServerResponse实现了stream.Writable接口的一个版本,但没有继承它的实际实现。 So when you call response.write() you're calling http.ServerResponse.write() and not stream.Writable.write() . 因此,当您调用response.write()您将调用http.ServerResponse.write()而不是stream.Writable.write()

Class: http.ServerResponse 类:http.ServerResponse

The response implements, but does not inherit from, the Writable Stream interface. 响应实现但不继承Writable Stream接口。

Internally, the http.ServerResponse is actually an OutgoingMessage , which is a Stream , and that supplies write() . 在内部, http.ServerResponse实际上是一个OutgoingMessage ,它是一个Stream ,并提供write() If you compare this against the stream.Writable.write() implementation, the way they going about writing the data is different. 如果将它与stream.Writable.write()实现进行比较,它们编写数据的方式就不同了。

I was also curious what benefits it gets in implementing writable stream if it uses its own write for example, etc? 我也很好奇它在实现可写流时有什么好处,如果它使用自己的写例如等等? (From Comments below) (来自下面的评论)

Without being part of the Node.js Foundation I can't speak to the motivation but, just from a design standpoint, implementing the stream.Writable interface provides familiar and common set of methods and events for developers to work with. 如果不是Node.js基金会的一部分,我就无法说出动机,但是从设计的角度来看,实现流stream.Writable接口为开发人员提供了熟悉且通用的方法和事件集。 Additionally, http.ServerResponse is writing to the connection socket (see OutgoingMessage._send() ) which isn't part of stream.Writable.write() implementation. 另外, http.ServerResponse正在写入连接套接字(请参阅OutgoingMessage._send() ),该套接字不是stream.Writable.write()实现的一部分。 Perhaps the below excerpt provides some insight into the thought process of the Foundation 也许下面的摘录提供了对基金会思想过程的一些见解

Writable Streams 可写流

All Writable streams implement the interface defined by the stream.Writable class. 所有可写流都实现了stream.Writable类定义的接口。

While specific instances of Writable streams may differ in various ways, all Writable streams follow the same fundamental usage pattern 虽然可写流的特定实例可能以各种方式不同,但所有可写流都遵循相同的基本使用模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM