outgoingMessage.write(chunk[, encoding][, callback])


如果调用这个方法并且没有发送标头,则会调用 this._implicitHeader 来刷新隐式标头。 如果消息不应该有正文(由 this._hasBody 指示),则忽略调用并且不会发送 chunk。 在处理不能包含正文的特定消息时,它可能很有用。 例如响应 HEAD 请求,204304 响应。

chunk 可以是字符串或缓冲区。 当 chunk 为字符串时,则 encoding 参数指定如何将 chunk 编码为字节流。 callback 将在 chunk 被刷新时被调用。

如果消息以块编码(由 this.chunkedEncoding 表示)传输,则 chunk 将作为块流中的一个块被刷新。 否则,它将作为消息正文刷新。

此方法处理 HTTP 消息的原始正文,与可能使用的更高级别的多部分正文编码无关。

如果是第一次调用消息的这个方法,则会先发送缓冲的标头,然后如上所述刷新 chunk

对该方法的第二次和后续调用将假定数据将被流式传输并单独发送新数据。 这意味着响应被缓冲到正文的第一个块。

如果整个数据被成功刷新到内核缓冲区,则返回 true。 如果所有或部分数据在用户内存中排队,则返回 false。 当缓冲区再次空闲时,将触发事件 drain

If this method is called and the header is not sent, it will call this._implicitHeader to flush implicit header. If the message should not have a body (indicated by this._hasBody), the call is ignored and chunk will not be sent. It could be useful when handling a particular message which must not include a body. e.g. response to HEAD request, 204 and 304 response.

chunk can be a string or a buffer. When chunk is a string, the encoding parameter specifies how to encode chunk into a byte stream. callback will be called when the chunk is flushed.

If the message is transferred in chucked encoding (indicated by this.chunkedEncoding), chunk will be flushed as one chunk among a stream of chunks. Otherwise, it will be flushed as the body of message.

This method handles the raw body of the HTTP message and has nothing to do with higher-level multi-part body encodings that may be used.

If it is the first call to this method of a message, it will send the buffered header first, then flush the chunk as described above.

The second and successive calls to this method will assume the data will be streamed and send the new data separately. It means that the response is buffered up to the first chunk of the body.

Returns true if the entire data was flushed successfully to the kernel buffer. Returns false if all or part of the data was queued in the user memory. Event drain will be emitted when the buffer is free again.