response.write(chunk[, encoding][, callback])
-
chunk
<string> | <Buffer> | <Uint8Array> -
encoding
<string> 默认值:'utf8'
¥
encoding
<string> Default:'utf8'
-
callback
<Function> -
返回:<boolean>
¥Returns: <boolean>
如果此方法被调用且 response.writeHead()
还没被调用,则会切换到隐式的标头模式并刷新隐式的标头。
¥If this method is called and response.writeHead()
has not been called,
it will switch to implicit header mode and flush the implicit headers.
这会发送一块响应正文。可以多次调用此方法以提供正文的连续部分。
¥This sends a chunk of the response body. This method may be called multiple times to provide successive parts of the body.
如果 createServer
中的 rejectNonStandardBodyWrites
设置为 true,则当请求方法或响应状态不支持内容时不允许写入正文。如果尝试写入正文以获取 HEAD 请求或作为 204
或 304
响应的一部分,则会抛出代码为 ERR_HTTP_BODY_NOT_ALLOWED
的同步 Error
。
¥If rejectNonStandardBodyWrites
is set to true in createServer
then writing to the body is not allowed when the request method or response
status do not support content. If an attempt is made to write to the body for a
HEAD request or as part of a 204
or 304
response, a synchronous Error
with the code ERR_HTTP_BODY_NOT_ALLOWED
is thrown.
chunk
可以是字符串或缓冲区。如果 chunk
是字符串,则第二个参数指定如何将其编码为字节流。当刷新数据块时将调用 callback
。
¥chunk
can be a string or a buffer. If chunk
is a string,
the second parameter specifies how to encode it into a byte stream.
callback
will be called when this chunk of data is flushed.
这是原始的 HTTP 正文,与可能使用的更高级别的多部分正文编码无关。
¥This is the raw HTTP body and has nothing to do with higher-level multi-part body encodings that may be used.
第一次调用 response.write()
时,它会将缓存的标头信息和正文的第一个块发送给客户端。第二次调用 response.write()
时,Node.js 会假定数据将被流式传输,并单独发送新数据。也就是说,响应被缓冲到正文的第一个块。
¥The first time response.write()
is called, it will send the buffered
header information and the first chunk of the body to the client. The second
time response.write()
is called, Node.js assumes data will be streamed,
and sends the new data separately. That is, the response is buffered up to the
first chunk of the body.
如果整个数据被成功刷新到内核缓冲区,则返回 true
。如果所有或部分数据在用户内存中排队,则返回 false
。当缓冲区再次空闲时,则将触发 'drain'
。
¥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 user memory.
'drain'
will be emitted when the buffer is free again.