http.ClientRequest 类


此对象由 http.request() 内部创建并返回。 它表示正在进行中的请求(请求头已进入队列)。 请求头仍然可以使用 setHeader(name, value)getHeader(name)removeHeader(name) API 进行改变。 实际的请求头会与第一个数据块一起发送,或者当调用 request.end() 时发送。

若要获取响应,则添加 'response' 事件监听器到请求对象。 当响应头已被接收时,则会从请求对象中触发 'response' 事件。 'response' 事件被执行时有一个参数,该参数是 http.IncomingMessage 的实例。

'response' 事件期间,可以添加监听器到响应对象,比如监听 'data' 事件。

如果没有添加 'response' 事件处理函数,则响应会被完全地丢弃。 但是,如果添加了 'response' 事件处理函数,则响应对象中的数据必须被消费(每当有 'readable' 事件时调用 response.read()、或添加 'data' 事件处理函数、或调用 .resume() 方法)。 在数据被消费完之前,不会触发 'end' 事件。 此外,在读取数据之前,它会占用内存,这可能最终会导致进程内存不足的错误。

request 对象不同,如果响应过早地关闭,则 response 对象不会触发 'error' 事件而是触发 'aborted' 事件。

Node.js 不会检查 Content-Length 与已传输的请求体的长度是否相等。

This object is created internally and returned from http.request(). It represents an in-progress request whose header has already been queued. The header is still mutable using the setHeader(name, value), getHeader(name), removeHeader(name) API. The actual header will be sent along with the first data chunk or when calling request.end().

To get the response, add a listener for 'response' to the request object. 'response' will be emitted from the request object when the response headers have been received. The 'response' event is executed with one argument which is an instance of http.IncomingMessage.

During the 'response' event, one can add listeners to the response object; particularly to listen for the 'data' event.

If no 'response' handler is added, then the response will be entirely discarded. However, if a 'response' event handler is added, then the data from the response object must be consumed, either by calling response.read() whenever there is a 'readable' event, or by adding a 'data' handler, or by calling the .resume() method. Until the data is consumed, the 'end' event will not fire. Also, until the data is read it will consume memory that can eventually lead to a 'process out of memory' error.

Unlike the request object, if the response closes prematurely, the response object does not emit an 'error' event but instead emits the 'aborted' event.

Node.js does not check whether Content-Length and the length of the body which has been transmitted are equal or not.