OutgoingMessage - Node documentation
class OutgoingMessage
extends stream.Writable

Usage in Deno

import { OutgoingMessage } from "node:http";

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

new
OutgoingMessage()

Type Parameters

Properties

deprecated
readonly
connection: Socket | null

Alias of outgoingMessage.socket.

deprecated
finished: boolean
readonly
headersSent: boolean

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

readonly
req: Request
sendDate: boolean
readonly
socket: Socket | null

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

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

Methods

addTrailers(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.

appendHeader(
name: string,
value: string | readonly string[],
): this

Append a single header value for the header object.

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

If there were no previous value for the header, this is equivalent of 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 ; .

flushHeaders(): void

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.

getHeader(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.

getHeaderNames(): string[]

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'] }
hasHeader(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');
removeHeader(name: string): void

Removes a header that is queued for implicit sending.

outgoingMessage.removeHeader('Content-Encoding');
setHeader(
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.

setTimeout(
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.