Skip to main content

OutgoingMessage

class OutgoingMessage
extends stream.Writable

This class serves as the parent class of ClientRequest and ServerResponse. It is an abstract outgoing message from the perspective of the participants of an HTTP transaction.

Constructors Jump to heading

new
Jump to headingOutgoingMessage()

Type Parameters Jump to heading

Properties Jump to heading

deprecated
readonly
Jump to headingconnection: Socket | null

Alias of outgoingMessage.socket.

deprecated
Jump to headingfinished: boolean

Read-only. true if the headers were sent, otherwise false.

Reference to the underlying socket. Usually, users will not want to access this property.

After calling outgoingMessage.end(), this property will be nulled.

Jump to headingaddTrailers(headers: OutgoingHttpHeaders | ReadonlyArray<[string, string]>): void

Adds HTTP trailers (headers but at the end of the message) to the message.

Trailers will only be emitted if the message is chunked encoded. If not, the trailers will be silently discarded.

HTTP requires the Trailer header to be sent to emit trailers, with a list of header field names in its value, e.g.

message.writeHead(200, { 'Content-Type': 'text/plain',
                         'Trailer': 'Content-MD5' });
message.write(fileData);
message.addTrailers({ 'Content-MD5': '7895bf4b8828b55ceaf47747b4bca667' });
message.end();

Attempting to set a header field name or value that contains invalid characters will result in a TypeError being thrown.

Jump to headingappendHeader(
name: string,
value: string | readonly string[],
): this

Append a single header value to the header object.

If the value is an array, this is equivalent to calling this method multiple times.

If there were no previous values for the header, this is equivalent to calling outgoingMessage.setHeader(name, value).

Depending of the value of options.uniqueHeaders when the client request or the server were created, this will end up in the header being sent multiple times or a single time with values joined using ; .

Flushes the message headers.

For efficiency reason, Node.js normally buffers the message headers until outgoingMessage.end() is called or the first chunk of message data is written. It then tries to pack the headers and data into a single TCP packet.

It is usually desired (it saves a TCP round-trip), but not when the first data is not sent until possibly much later. outgoingMessage.flushHeaders() bypasses the optimization and kickstarts the message.

Jump to headinggetHeader(name: string):
number
| string
| string[]
| undefined

Gets the value of the HTTP header with the given name. If that header is not set, the returned value will be undefined.

Returns an array containing the unique names of the current outgoing headers. All names are lowercase.

Returns a shallow copy of the current outgoing headers. Since a shallow copy is used, array values may be mutated without additional calls to various header-related HTTP module methods. The keys of the returned object are the header names and the values are the respective header values. All header names are lowercase.

The object returned by the outgoingMessage.getHeaders() method does not prototypically inherit from the JavaScript Object. This means that typical Object methods such as obj.toString(), obj.hasOwnProperty(), and others are not defined and will not work.

outgoingMessage.setHeader('Foo', 'bar');
outgoingMessage.setHeader('Set-Cookie', ['foo=bar', 'bar=baz']);

const headers = outgoingMessage.getHeaders();
// headers === { foo: 'bar', 'set-cookie': ['foo=bar', 'bar=baz'] }
Jump to headinghasHeader(name: string): boolean

Returns true if the header identified by name is currently set in the outgoing headers. The header name is case-insensitive.

const hasContentType = outgoingMessage.hasHeader('content-type');
Jump to headingremoveHeader(name: string): void

Removes a header that is queued for implicit sending.

outgoingMessage.removeHeader('Content-Encoding');
Jump to headingsetHeader(
name: string,
value:
number
| string
| readonly string[]
,
): this

Sets a single header value. If the header already exists in the to-be-sent headers, its value will be replaced. Use an array of strings to send multiple headers with the same name.

Jump to headingsetTimeout(
msecs: number,
callback?: () => void,
): this

Once a socket is associated with the message and is connected, socket.setTimeout() will be called with msecs as the first parameter.

Back to top