Skip to main content
Http2ServerResponse - http2 - Node documentation
class Http2ServerResponse
extends stream.Writable

Usage in Deno

import { Http2ServerResponse } from "node:http2";

This object is created internally by an HTTP server, not by the user. It is passed as the second parameter to the 'request' event.

Constructors

new
Http2ServerResponse(stream: ServerHttp2Stream)

Type Parameters

Properties

deprecated
readonly
connection: net.Socket | tls.TLSSocket

See response.socket.

deprecated
readonly
finished: boolean

Boolean value that indicates whether the response has completed. Starts as false. After response.end() executes, the value will be true.

readonly
headersSent: boolean

True if headers were sent, false otherwise (read-only).

readonly
req: Request

A reference to the original HTTP2 request object.

sendDate: boolean

When true, the Date header will be automatically generated and sent in the response if it is not already present in the headers. Defaults to true.

This should only be disabled for testing; HTTP requires the Date header in responses.

readonly
socket: net.Socket | tls.TLSSocket

Returns a Proxy object that acts as a net.Socket (or tls.TLSSocket) but applies getters, setters, and methods based on HTTP/2 logic.

destroyed, readable, and writable properties will be retrieved from and set on response.stream.

destroy, emit, end, on and once methods will be called on response.stream.

setTimeout method will be called on response.stream.session.

pause, read, resume, and write will throw an error with code ERR_HTTP2_NO_SOCKET_MANIPULATION. See Http2Session and Sockets for more information.

All other interactions will be routed directly to the socket.

import http2 from 'node:http2';
const server = http2.createServer((req, res) => {
  const ip = req.socket.remoteAddress;
  const port = req.socket.remotePort;
  res.end(`Your IP address is ${ip} and your source port is ${port}.`);
}).listen(3000);
statusCode: number

When using implicit headers (not calling response.writeHead() explicitly), this property controls the status code that will be sent to the client when the headers get flushed.

response.statusCode = 404;

After response header was sent to the client, this property indicates the status code which was sent out.

Status message is not supported by HTTP/2 (RFC 7540 8.1.2.4). It returns an empty string.

The Http2Stream object backing the response.

Methods

addListener(
event: "close",
listener: () => void,
): this
addListener(
event: "drain",
listener: () => void,
): this
addListener(
event: "error",
listener: (error: Error) => void,
): this
addListener(
event: "finish",
listener: () => void,
): this
addListener(
event: "pipe",
listener: (src: stream.Readable) => void,
): this
addListener(
event: "unpipe",
listener: (src: stream.Readable) => void,
): this
addListener(
event: string | symbol,
listener: (...args: any[]) => void,
): this

This method adds HTTP trailing headers (a header but at the end of the message) to the response.

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 | string[],
): void

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 setHeader.

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

// Returns headers including "set-cookie: a" and "set-cookie: b"
const server = http2.createServer((req, res) => {
  res.setHeader('set-cookie', 'a');
  res.appendHeader('set-cookie', 'b');
  res.writeHead(200);
  res.end('ok');
});
createPushResponse(
callback: (
err: Error | null,
) => void
,
): void

Call http2stream.pushStream() with the given headers, and wrap the given Http2Stream on a newly created Http2ServerResponse as the callback parameter if successful. When Http2ServerRequest is closed, the callback is called with an error ERR_HTTP2_INVALID_STREAM.

emit(event: "close"): boolean
emit(event: "drain"): boolean
emit(
event: "error",
error: Error,
): boolean
emit(event: "finish"): boolean
emit(
event: "pipe",
src: stream.Readable,
): boolean
emit(
event: "unpipe",
src: stream.Readable,
): boolean
emit(
event: string | symbol,
...args: any[],
): boolean
end(callback?: () => void): this

This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. The method, response.end(), MUST be called on each response.

If data is specified, it is equivalent to calling response.write(data, encoding) followed by response.end(callback).

If callback is specified, it will be called when the response stream is finished.

end(
data: string | Uint8Array,
callback?: () => void,
): this
end(
data: string | Uint8Array,
encoding: BufferEncoding,
callback?: () => void,
): this
getHeader(name: string): string

Reads out a header that has already been queued but not sent to the client. The name is case-insensitive.

const contentType = response.getHeader('content-type');
getHeaderNames(): string[]

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

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

const headerNames = response.getHeaderNames();
// headerNames === ['foo', 'set-cookie']

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 response.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.

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

const headers = response.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 matching is case-insensitive.

const hasContentType = response.hasHeader('content-type');
on(
event: "close",
listener: () => void,
): this
on(
event: "drain",
listener: () => void,
): this
on(
event: "error",
listener: (error: Error) => void,
): this
on(
event: "finish",
listener: () => void,
): this
on(
event: "pipe",
listener: (src: stream.Readable) => void,
): this
on(
event: "unpipe",
listener: (src: stream.Readable) => void,
): this
on(
event: string | symbol,
listener: (...args: any[]) => void,
): this
once(
event: "close",
listener: () => void,
): this
once(
event: "drain",
listener: () => void,
): this
once(
event: "error",
listener: (error: Error) => void,
): this
once(
event: "finish",
listener: () => void,
): this
once(
event: "pipe",
listener: (src: stream.Readable) => void,
): this
once(
event: "unpipe",
listener: (src: stream.Readable) => void,
): this
once(
event: string | symbol,
listener: (...args: any[]) => void,
): this
prependListener(
event: "close",
listener: () => void,
): this
prependListener(
event: "drain",
listener: () => void,
): this
prependListener(
event: "error",
listener: (error: Error) => void,
): this
prependListener(
event: "finish",
listener: () => void,
): this
prependListener(
event: "pipe",
listener: (src: stream.Readable) => void,
): this
prependListener(
event: "unpipe",
listener: (src: stream.Readable) => void,
): this
prependListener(
event: string | symbol,
listener: (...args: any[]) => void,
): this
prependOnceListener(
event: "close",
listener: () => void,
): this
prependOnceListener(
event: "drain",
listener: () => void,
): this
prependOnceListener(
event: "error",
listener: (error: Error) => void,
): this
prependOnceListener(
event: "finish",
listener: () => void,
): this
prependOnceListener(
event: "pipe",
listener: (src: stream.Readable) => void,
): this
prependOnceListener(
event: "unpipe",
listener: (src: stream.Readable) => void,
): this
prependOnceListener(
event: string | symbol,
listener: (...args: any[]) => void,
): this
removeHeader(name: string): void

Removes a header that has been queued for implicit sending.

response.removeHeader('Content-Encoding');
setHeader(
name: string,
value:
number
| string
| readonly string[]
,
): void

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

response.setHeader('Content-Type', 'text/html; charset=utf-8');

or

response.setHeader('Set-Cookie', ['type=ninja', 'language=javascript']);

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

When headers have been set with response.setHeader(), they will be merged with any headers passed to response.writeHead(), with the headers passed to response.writeHead() given precedence.

// Returns content-type = text/plain
const server = http2.createServer((req, res) => {
  res.setHeader('Content-Type', 'text/html; charset=utf-8');
  res.setHeader('X-Foo', 'bar');
  res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  res.end('ok');
});
setTimeout(
msecs: number,
callback?: () => void,
): void

Sets the Http2Stream's timeout value to msecs. If a callback is provided, then it is added as a listener on the 'timeout' event on the response object.

If no 'timeout' listener is added to the request, the response, or the server, then Http2Stream s are destroyed when they time out. If a handler is assigned to the request, the response, or the server's 'timeout' events, timed out sockets must be handled explicitly.

write(
chunk: string | Uint8Array,
callback?: (err: Error) => void,
): boolean

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.

In the node:http module, the response body is omitted when the request is a HEAD request. Similarly, the 204 and 304 responses must not include a message body.

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. By default the encoding is 'utf8'. callback will be called when this chunk of data is flushed.

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

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.

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.

write(
chunk: string | Uint8Array,
encoding: BufferEncoding,
callback?: (err: Error) => void,
): boolean

Sends a status 100 Continue to the client, indicating that the request body should be sent. See the 'checkContinue' event on Http2Server and Http2SecureServer.

writeEarlyHints(hints: Record<string, string | string[]>): void

Sends a status 103 Early Hints to the client with a Link header, indicating that the user agent can preload/preconnect the linked resources. The hints is an object containing the values of headers to be sent with early hints message.

Example

const earlyHintsLink = '</styles.css>; rel=preload; as=style';
response.writeEarlyHints({
  'link': earlyHintsLink,
});

const earlyHintsLinks = [
  '</styles.css>; rel=preload; as=style',
  '</scripts.js>; rel=preload; as=script',
];
response.writeEarlyHints({
  'link': earlyHintsLinks,
});
writeHead(
statusCode: number,
): this

Sends a response header to the request. The status code is a 3-digit HTTP status code, like 404. The last argument, headers, are the response headers.

Returns a reference to the Http2ServerResponse, so that calls can be chained.

For compatibility with HTTP/1, a human-readable statusMessage may be passed as the second argument. However, because the statusMessage has no meaning within HTTP/2, the argument will have no effect and a process warning will be emitted.

const body = 'hello world';
response.writeHead(200, {
  'Content-Length': Buffer.byteLength(body),
  'Content-Type': 'text/plain; charset=utf-8',
});

Content-Length is given in bytes not characters. TheBuffer.byteLength() API may be used to determine the number of bytes in a given encoding. On outbound messages, Node.js does not check if Content-Length and the length of the body being transmitted are equal or not. However, when receiving messages, Node.js will automatically reject messages when the Content-Length does not match the actual payload size.

This method may be called at most one time on a message before response.end() is called.

If response.write() or response.end() are called before calling this, the implicit/mutable headers will be calculated and call this function.

When headers have been set with response.setHeader(), they will be merged with any headers passed to response.writeHead(), with the headers passed to response.writeHead() given precedence.

// Returns content-type = text/plain
const server = http2.createServer((req, res) => {
  res.setHeader('Content-Type', 'text/html; charset=utf-8');
  res.setHeader('X-Foo', 'bar');
  res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  res.end('ok');
});

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

writeHead(
statusCode: number,
statusMessage: string,
): this