HTTP/2
- HTTP/2
- Determining if crypto support is unavailable
- Core API
- Server-side example
- Client-side example
- Class: Http2Session
- Http2Session and sockets
- Event: 'close'
- Event: 'connect'
- Event: 'error'
- Event: 'frameError'
- Event: 'goaway'
- Event: 'localSettings'
- Event: 'ping'
- Event: 'remoteSettings'
- Event: 'stream'
- Event: 'timeout'
- http2session.alpnProtocol
- http2session.close([callback])
- http2session.closed
- http2session.connecting
- http2session.destroy([error][, code])
- http2session.destroyed
- http2session.encrypted
- http2session.goaway([code[, lastStreamID[, opaqueData]]])
- http2session.localSettings
- http2session.originSet
- http2session.pendingSettingsAck
- http2session.ping([payload, ]callback)
- http2session.ref()
- http2session.remoteSettings
- http2session.setLocalWindowSize(windowSize)
- http2session.setTimeout(msecs, callback)
- http2session.socket
- http2session.state
- http2session.settings([settings][, callback])
- http2session.type
- http2session.unref()
- Class: ServerHttp2Session
- Class: ClientHttp2Session
- Class: Http2Stream
- Http2Stream Lifecycle
- Event: 'aborted'
- Event: 'close'
- Event: 'error'
- Event: 'frameError'
- Event: 'ready'
- Event: 'timeout'
- Event: 'trailers'
- Event: 'wantTrailers'
- http2stream.aborted
- http2stream.bufferSize
- http2stream.close(code[, callback])
- http2stream.closed
- http2stream.destroyed
- http2stream.endAfterHeaders
- http2stream.id
- http2stream.pending
- http2stream.priority(options)
- http2stream.rstCode
- http2stream.sentHeaders
- http2stream.sentInfoHeaders
- http2stream.sentTrailers
- http2stream.session
- http2stream.setTimeout(msecs, callback)
<!-- YAML
added: v8.4.0
changes:
- version:
- v15.3.0
- v14.17.0
pr-url: https://github.com/nodejs/node/pull/36070
description: It is possible to abort a request with an AbortSignal.
- version: v15.0.0
pr-url: https://github.com/nodejs/node/pull/34664
description: Requests with the host header (with or without
:authority) can now be sent/received.
- version: v10.10.0
pr-url: https://github.com/nodejs/node/pull/22466
description: HTTP/2 is now Stable. Previously, it had been Experimental.
-->
<!--introduced_in=v8.4.0-->
Stability: 2 - Stable
<!-- source_link=lib/http2.js -->
The node:http2 module provides an implementation of the [HTTP/2][] protocol.
It can be accessed using:
const http2 = require('node:http2');
Determining if crypto support is unavailable
It is possible for Node.js to be built without including support for the
node:crypto module. In such cases, attempting to import from node:http2 or
calling require('node:http2') will result in an error being thrown.
When using CommonJS, the error thrown can be caught using try/catch:
let http2;
try {
http2 = require('node:http2');
} catch (err) {
console.error('http2 support is disabled!');
}
When using the lexical ESM import keyword, the error can only be
caught if a handler for process.on('uncaughtException') is registered
before any attempt to load the module is made (using, for instance,
a preload module).
When using ESM, if there is a chance that the code may be run on a build
of Node.js where crypto support is not enabled, consider using the
[import()][] function instead of the lexical import keyword:
let http2;
try {
http2 = await import('node:http2');
} catch (err) {
console.error('http2 support is disabled!');
}
Core API
The Core API provides a low-level interface designed specifically around support for HTTP/2 protocol features. It is specifically not designed for compatibility with the existing [HTTP/1][] module API. However, the [Compatibility API][] is.
The http2 Core API is much more symmetric between client and server than the
http API. For instance, most events, like 'error', 'connect' and
'stream', can be emitted either by client-side code or server-side code.
Server-side example
The following illustrates a simple HTTP/2 server using the Core API.
Since there are no browsers known that support
[unencrypted HTTP/2][HTTP/2 Unencrypted], the use of
[http2.createSecureServer()][] is necessary when communicating
with browser clients.
import { createSecureServer } from 'node:http2';
import { readFileSync } from 'node:fs';
const server = createSecureServer({
key: readFileSync('localhost-privkey.pem'),
cert: readFileSync('localhost-cert.pem'),
});
server.on('error', (err) => console.error(err));
server.on('stream', (stream, headers) => {
// stream is a Duplex
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 200,
});
stream.end('<h1>Hello World</h1>');
});
server.listen(8443);
const http2 = require('node:http2');
const fs = require('node:fs');
const server = http2.createSecureServer({
key: fs.readFileSync('localhost-privkey.pem'),
cert: fs.readFileSync('localhost-cert.pem'),
});
server.on('error', (err) => console.error(err));
server.on('stream', (stream, headers) => {
// stream is a Duplex
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 200,
});
stream.end('<h1>Hello World</h1>');
});
server.listen(8443);
To generate the certificate and key for this example, run:
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost' \
-keyout localhost-privkey.pem -out localhost-cert.pem
Client-side example
The following illustrates an HTTP/2 client:
import { connect } from 'node:http2';
import { readFileSync } from 'node:fs';
const client = connect('https://localhost:8443', {
ca: readFileSync('localhost-cert.pem'),
});
client.on('error', (err) => console.error(err));
const req = client.request({ ':path': '/' });
req.on('response', (headers, flags) => {
for (const name in headers) {
console.log(`${name}: ${headers[name]}`);
}
});
req.setEncoding('utf8');
let data = '';
req.on('data', (chunk) => { data += chunk; });
req.on('end', () => {
console.log(`\n${data}`);
client.close();
});
req.end();
const http2 = require('node:http2');
const fs = require('node:fs');
const client = http2.connect('https://localhost:8443', {
ca: fs.readFileSync('localhost-cert.pem'),
});
client.on('error', (err) => console.error(err));
const req = client.request({ ':path': '/' });
req.on('response', (headers, flags) => {
for (const name in headers) {
console.log(`${name}: ${headers[name]}`);
}
});
req.setEncoding('utf8');
let data = '';
req.on('data', (chunk) => { data += chunk; });
req.on('end', () => {
console.log(`\n${data}`);
client.close();
});
req.end();
Class: Http2Session
<!-- YAML added: v8.4.0 -->
- Extends: {EventEmitter}
Instances of the http2.Http2Session class represent an active communications
session between an HTTP/2 client and server. Instances of this class are not
intended to be constructed directly by user code.
Each Http2Session instance will exhibit slightly different behaviors
depending on whether it is operating as a server or a client. The
http2session.type property can be used to determine the mode in which an
Http2Session is operating. On the server side, user code should rarely
have occasion to work with the Http2Session object directly, with most
actions typically taken through interactions with either the Http2Server or
Http2Stream objects.
User code will not create Http2Session instances directly. Server-side
Http2Session instances are created by the Http2Server instance when a
new HTTP/2 connection is received. Client-side Http2Session instances are
created using the http2.connect() method.
Http2Session and sockets
Every Http2Session instance is associated with exactly one [net.Socket][] or
[tls.TLSSocket][] when it is created. When either the Socket or the
Http2Session are destroyed, both will be destroyed.
Because of the specific serialization and processing requirements imposed
by the HTTP/2 protocol, it is not recommended for user code to read data from
or write data to a Socket instance bound to a Http2Session. Doing so can
put the HTTP/2 session into an indeterminate state causing the session and
the socket to become unusable.
Once a Socket has been bound to an Http2Session, user code should rely
solely on the API of the Http2Session.
Event: 'close'
<!-- YAML added: v8.4.0 -->
The 'close' event is emitted once the Http2Session has been destroyed. Its
listener does not expect any arguments.
Event: 'connect'
<!-- YAML added: v8.4.0 -->
session{Http2Session}socket{net.Socket}
The 'connect' event is emitted once the Http2Session has been successfully
connected to the remote peer and communication may begin.
User code will typically not listen for this event directly.
Event: 'error'
<!-- YAML added: v8.4.0 -->
error{Error}
The 'error' event is emitted when an error occurs during the processing of
an Http2Session.
Event: 'frameError'
<!-- YAML added: v8.4.0 -->
type{integer} The frame type.code{integer} The error code.id{integer} The stream id (or0if the frame isn't associated with a stream).
The 'frameError' event is emitted when an error occurs while attempting to
send a frame on the session. If the frame that could not be sent is associated
with a specific Http2Stream, an attempt to emit a 'frameError' event on the
Http2Stream is made.
If the 'frameError' event is associated with a stream, the stream will be
closed and destroyed immediately following the 'frameError' event. If the
event is not associated with a stream, the Http2Session will be shut down
immediately following the 'frameError' event.
Event: 'goaway'
<!-- YAML added: v8.4.0 -->
errorCode{number} The HTTP/2 error code specified in theGOAWAYframe.lastStreamID{number} The ID of the last stream the remote peer successfully processed (or0if no ID is specified).opaqueData{Buffer} If additional opaque data was included in theGOAWAYframe, aBufferinstance will be passed containing that data.
The 'goaway' event is emitted when a GOAWAY frame is received.
The Http2Session instance will be shut down automatically when the 'goaway'
event is emitted.
Event: 'localSettings'
<!-- YAML added: v8.4.0 -->
settings{HTTP/2 Settings Object} A copy of theSETTINGSframe received.
The 'localSettings' event is emitted when an acknowledgment SETTINGS frame
has been received.
When using http2session.settings() to submit new settings, the modified
settings do not take effect until the 'localSettings' event is emitted.
session.settings({ enablePush: false });
session.on('localSettings', (settings) => {
/* Use the new settings */
});
Event: 'ping'
<!-- YAML added: v10.12.0 -->
payload{Buffer} ThePINGframe 8-byte payload
The 'ping' event is emitted whenever a PING frame is received from the
connected peer.
Event: 'remoteSettings'
<!-- YAML added: v8.4.0 -->
settings{HTTP/2 Settings Object} A copy of theSETTINGSframe received.
The 'remoteSettings' event is emitted when a new SETTINGS frame is received
from the connected peer.
session.on('remoteSettings', (settings) => {
/* Use the new settings */
});
Event: 'stream'
<!-- YAML added: v8.4.0 -->
stream{Http2Stream} A reference to the streamheaders{HTTP/2 Headers Object} An object describing the headersflags{number} The associated numeric flagsrawHeaders{HTTP/2 Raw Headers} An array containing the raw headers
The 'stream' event is emitted when a new Http2Stream is created.
session.on('stream', (stream, headers, flags) => {
const method = headers[':method'];
const path = headers[':path'];
// ...
stream.respond({
':status': 200,
'content-type': 'text/plain; charset=utf-8',
});
stream.write('hello ');
stream.end('world');
});
On the server side, user code will typically not listen for this event directly,
and would instead register a handler for the 'stream' event emitted by the
net.Server or tls.Server instances returned by http2.createServer() and
http2.createSecureServer(), respectively, as in the example below:
import { createServer } from 'node:http2';
// Create an unencrypted HTTP/2 server
const server = createServer();
server.on('stream', (stream, headers) => {
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 200,
});
stream.on('error', (error) => console.error(error));
stream.end('<h1>Hello World</h1>');
});
server.listen(8000);
const http2 = require('node:http2');
// Create an unencrypted HTTP/2 server
const server = http2.createServer();
server.on('stream', (stream, headers) => {
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 200,
});
stream.on('error', (error) => console.error(error));
stream.end('<h1>Hello World</h1>');
});
server.listen(8000);
Even though HTTP/2 streams and network sockets are not in a 1:1 correspondence, a network error will destroy each individual stream and must be handled on the stream level, as shown above.
Event: 'timeout'
<!-- YAML added: v8.4.0 -->
After the http2session.setTimeout() method is used to set the timeout period
for this Http2Session, the 'timeout' event is emitted if there is no
activity on the Http2Session after the configured number of milliseconds.
Its listener does not expect any arguments.
session.setTimeout(2000);
session.on('timeout', () => { /* .. */ });
http2session.alpnProtocol
<!-- YAML added: v9.4.0 -->
- Type: {string|undefined}
Value will be undefined if the Http2Session is not yet connected to a
socket, h2c if the Http2Session is not connected to a TLSSocket, or
will return the value of the connected TLSSocket's own alpnProtocol
property.
http2session.close([callback])
<!-- YAML added: v9.4.0 -->
callback{Function}
Gracefully closes the Http2Session, allowing any existing streams to
complete on their own and preventing new Http2Stream instances from being
created. Once closed, http2session.destroy() might be called if there
are no open Http2Stream instances.
If specified, the callback function is registered as a handler for the
'close' event.
http2session.closed
<!-- YAML added: v9.4.0 -->
- Type: {boolean}
Will be true if this Http2Session instance has been closed, otherwise
false.
http2session.connecting
<!-- YAML added: v10.0.0 -->
- Type: {boolean}
Will be true if this Http2Session instance is still connecting, will be set
to false before emitting connect event and/or calling the http2.connect
callback.
http2session.destroy([error][, code])
<!-- YAML added: v8.4.0 -->
error{Error} AnErrorobject if theHttp2Sessionis being destroyed due to an error.code{number} The HTTP/2 error code to send in the finalGOAWAYframe. If unspecified, anderroris not undefined, the default isINTERNAL_ERROR, otherwise defaults toNO_ERROR.
Immediately terminates the Http2Session and the associated net.Socket or
tls.TLSSocket.
Once destroyed, the Http2Session will emit the 'close' event. If error
is not undefined, an 'error' event will be emitted immediately before the
'close' event.
If there are any remaining open Http2Streams associated with the
Http2Session, those will also be destroyed.
http2session.destroyed
<!-- YAML added: v8.4.0 -->
- Type: {boolean}
Will be true if this Http2Session instance has been destroyed and must no
longer be used, otherwise false.
http2session.encrypted
<!-- YAML added: v9.4.0 -->
- Type: {boolean|undefined}
Value is undefined if the Http2Session session socket has not yet been
connected, true if the Http2Session is connected with a TLSSocket,
and false if the Http2Session is connected to any other kind of socket
or stream.
http2session.goaway([code[, lastStreamID[, opaqueData]]])
<!-- YAML added: v9.4.0 -->
code{number} An HTTP/2 error codelastStreamID{number} The numeric ID of the last processedHttp2StreamopaqueData{Buffer|TypedArray|DataView} ATypedArrayorDataViewinstance containing additional data to be carried within theGOAWAYframe.
Transmits a GOAWAY frame to the connected peer without shutting down the
Http2Session.
http2session.localSettings
<!-- YAML added: v8.4.0 -->
- Type: {HTTP/2 Settings Object}
A prototype-less object describing the current local settings of this
Http2Session. The local settings are local to this Http2Session instance.
http2session.originSet
<!-- YAML added: v9.4.0 -->
- Type: {string[]|undefined}
If the Http2Session is connected to a TLSSocket, the originSet property
will return an Array of origins for which the Http2Session may be
considered authoritative.
The originSet property is only available when using a secure TLS connection.
http2session.pendingSettingsAck
<!-- YAML added: v8.4.0 -->
- Type: {boolean}
Indicates whether the Http2Session is currently waiting for acknowledgment of
a sent SETTINGS frame. Will be true after calling the
http2session.settings() method. Will be false once all sent SETTINGS
frames have been acknowledged.
http2session.ping([payload, ]callback)
<!-- YAML
added: v8.9.3
changes:
- version: v18.0.0
pr-url: https://github.com/nodejs/node/pull/41678
description: Passing an invalid callback to the callback argument
now throws ERR_INVALID_ARG_TYPE instead of
ERR_INVALID_CALLBACK.
-->
payload{Buffer|TypedArray|DataView} Optional ping payload.callback{Function}- Returns: {boolean}
Sends a PING frame to the connected HTTP/2 peer. A callback function must
be provided. The method will return true if the PING was sent, false
otherwise.
The maximum number of outstanding (unacknowledged) pings is determined by the
maxOutstandingPings configuration option. The default maximum is 10.
If provided, the payload must be a Buffer, TypedArray, or DataView
containing 8 bytes of data that will be transmitted with the PING and
returned with the ping acknowledgment.
The callback will be invoked with three arguments: an error argument that will
be null if the PING was successfully acknowledged, a duration argument
that reports the number of milliseconds elapsed since the ping was sent and the
acknowledgment was received, and a Buffer containing the 8-byte PING
payload.
session.ping(Buffer.from('abcdefgh'), (err, duration, payload) => {
if (!err) {
console.log(`Ping acknowledged in ${duration} milliseconds`);
console.log(`With payload '${payload.toString()}'`);
}
});
If the payload argument is not specified, the default payload will be the
64-bit timestamp (little endian) marking the start of the PING duration.
http2session.ref()
<!-- YAML added: v9.4.0 -->
Calls [ref()][net.Socket.prototype.ref()] on this Http2Session
instance's underlying [net.Socket][].
http2session.remoteSettings
<!-- YAML added: v8.4.0 -->
- Type: {HTTP/2 Settings Object}
A prototype-less object describing the current remote settings of this
Http2Session. The remote settings are set by the connected HTTP/2 peer.
http2session.setLocalWindowSize(windowSize)
<!-- YAML added: - v15.3.0 - v14.18.0 -->
windowSize{number}
Sets the local endpoint's connection-level window size.
The windowSize is the total window size to set, not
the delta.
Increases take effect immediately, but decreases only apply as the window
already advertised to the peer is consumed, since a window that has been
advertised cannot be retracted. To use a window smaller than the default from
the start of the connection, set the connectionWindowSize option when
creating the server or client session instead.
import { createServer } from 'node:http2';
const server = createServer();
const expectedWindowSize = 2 ** 20;
server.on('session', (session) => {
// Set local window size to be 2 ** 20
session.setLocalWindowSize(expectedWindowSize);
});
const http2 = require('node:http2');
const server = http2.createServer();
const expectedWindowSize = 2 ** 20;
server.on('session', (session) => {
// Set local window size to be 2 ** 20
session.setLocalWindowSize(expectedWindowSize);
});
For http2 clients the proper event is either 'connect' or 'remoteSettings'.
http2session.setTimeout(msecs, callback)
<!-- YAML
added: v8.4.0
changes:
- version: v18.0.0
pr-url: https://github.com/nodejs/node/pull/41678
description: Passing an invalid callback to the callback argument
now throws ERR_INVALID_ARG_TYPE instead of
ERR_INVALID_CALLBACK.
-->
msecs{number}callback{Function}
Used to set a callback function that is called when there is no activity on
the Http2Session after msecs milliseconds. The given callback is
registered as a listener on the 'timeout' event.
http2session.socket
<!-- YAML
added: v8.4.0
changes:
- version: v26.6.0
pr-url: https://github.com/nodejs/node/pull/64427
description: Calling destroy no longer throws and instead destroys the
Http2Session.
-->
- Type: {net.Socket|tls.TLSSocket}
Returns a Proxy object that acts as a net.Socket (or tls.TLSSocket) but
limits available methods to ones safe to use with HTTP/2.
emit, end, pause, read, resume, and write will throw
an error with code ERR_HTTP2_NO_SOCKET_MANIPULATION. See
[Http2Session and Sockets][] for more information.
destroy, setTimeout, ref, and unref methods will be called on this
Http2Session.
All other interactions will be routed directly to the socket.
http2session.state
<!-- YAML added: v8.4.0 -->
Provides miscellaneous information about the current state of the
Http2Session.
- Type: {Object}
effectiveLocalWindowSize{number} The current local (receive) flow control window size for theHttp2Session.effectiveRecvDataLength{number} The current number of bytes that have been received since the last flow controlWINDOW_UPDATE.nextStreamID{number} The numeric identifier to be used the next time a newHttp2Streamis created by thisHttp2Session.localWindowSize{number} The number of bytes that the remote peer can send without receiving aWINDOW_UPDATE.lastProcStreamID{number} The numeric id of theHttp2Streamfor which aHEADERSorDATAframe was most recently received.remoteWindowSize{number} The number of bytes that thisHttp2Sessionmay send without receiving aWINDOW_UPDATE.outboundQueueSize{number} The number of frames currently within the outbound queue for thisHttp2Session.deflateDynamicTableSize{number} The current size in bytes of the outbound header compression state table.inflateDynamicTableSize{number} The current size in bytes of the inbound header compression state table.
An object describing the current status of this Http2Session.
http2session.settings([settings][, callback])
<!-- YAML
added: v8.4.0
changes:
- version: v18.0.0
pr-url: https://github.com/nodejs/node/pull/41678
description: Passing an invalid callback to the callback argument
now throws ERR_INVALID_ARG_TYPE instead of
ERR_INVALID_CALLBACK.
-->
settings{HTTP/2 Settings Object}callback{Function} Callback that is called once the session is connected or right away if the session is already connected.err{Error|null}settings{HTTP/2 Settings Object} The updatedsettingsobject.duration{integer}
Updates the current local settings for this Http2Session and sends a new
SETTINGS frame to the connected HTTP/2 peer.
Once called, the http2session.pendingSettingsAck property will be true
while the session is waiting for the remote peer to acknowledge the new
settings.
The new settings will not become effective until the SETTINGS acknowledgment
is received and the 'localSettings' event is emitted. It is possible to send
multiple SETTINGS frames while acknowledgment is still pending.
http2session.type
<!-- YAML added: v8.4.0 -->
- Type: {number}
The http2session.type will be equal to
http2.constants.NGHTTP2_SESSION_SERVER if this Http2Session instance is a
server, and http2.constants.NGHTTP2_SESSION_CLIENT if the instance is a
client.
http2session.unref()
<!-- YAML added: v9.4.0 -->
Calls [unref()][net.Socket.prototype.unref()] on this Http2Session
instance's underlying [net.Socket][].
Class: ServerHttp2Session
<!-- YAML added: v8.4.0 -->
- Extends: {Http2Session}
serverhttp2session.altsvc(alt, originOrStream)
<!-- YAML added: v9.4.0 -->
alt{string} A description of the alternative service configuration as defined by [RFC 7838][].originOrStream{number|string|URL|Object} Either a URL string specifying the origin (or anObjectwith anoriginproperty) or the numeric identifier of an activeHttp2Streamas given by thehttp2stream.idproperty.
Submits an ALTSVC frame (as defined by [RFC 7838][]) to the connected client.
import { createServer } from 'node:http2';
const server = createServer();
server.on('session', (session) => {
// Set altsvc for origin https://example.org:80
session.altsvc('h2=":8000"', 'https://example.org:80');
});
server.on('stream', (stream) => {
// Set altsvc for a specific stream
stream.session.altsvc('h2=":8000"', stream.id);
});
const http2 = require('node:http2');
const server = http2.createServer();
server.on('session', (session) => {
// Set altsvc for origin https://example.org:80
session.altsvc('h2=":8000"', 'https://example.org:80');
});
server.on('stream', (stream) => {
// Set altsvc for a specific stream
stream.session.altsvc('h2=":8000"', stream.id);
});
Sending an ALTSVC frame with a specific stream ID indicates that the alternate
service is associated with the origin of the given Http2Stream.
The alt and origin string must contain only ASCII bytes and are
strictly interpreted as a sequence of ASCII bytes. The special value 'clear'
may be passed to clear any previously set alternative service for a given
domain.
When a string is passed for the originOrStream argument, it will be parsed as
a URL and the origin will be derived. For instance, the origin for the
HTTP URL 'https://example.org/foo/bar' is the ASCII string
'https://example.org'. An error will be thrown if either the given string
cannot be parsed as a URL or if a valid origin cannot be derived.
A URL object, or any object with an origin property, may be passed as
originOrStream, in which case the value of the origin property will be
used. The value of the origin property must be a properly serialized
ASCII origin.
Specifying alternative services
The format of the alt parameter is strictly defined by [RFC 7838][] as an
ASCII string containing a comma-delimited list of “alternative” protocols
associated with a specific host and port.
For example, the value 'h2=“example.org:81”' indicates that the HTTP/2
protocol is available on the host 'example.org' on TCP/IP port 81. The
host and port must be contained within the quote (“) characters.
Multiple alternatives may be specified, for instance: 'h2=“example.org:81”,
h2=“:82”'.
The protocol identifier ('h2' in the examples) may be any valid
[ALPN Protocol ID][].
The syntax of these values is not validated by the Node.js implementation and are passed through as provided by the user or received from the peer.
serverhttp2session.origin(...origins)
<!-- YAML added: v10.12.0 -->
origins{ string | URL | Object } One or more URL Strings passed as separate arguments.
Submits an ORIGIN frame (as defined by [RFC 8336][]) to the connected client
to advertise the set of origins for which the server is capable of providing
authoritative responses.
import { createSecureServer } from 'node:http2';
const options = getSecureOptionsSomehow();
const server = createSecureServer(options);
server.on('stream', (stream) => {
stream.respond();
stream.end('ok');
});
server.on('session', (session) => {
session.origin('https://example.com', 'https://example.org');
});
const http2 = require('node:http2');
const options = getSecureOptionsSomehow();
const server = http2.createSecureServer(options);
server.on('stream', (stream) => {
stream.respond();
stream.end('ok');
});
server.on('session', (session) => {
session.origin('https://example.com', 'https://example.org');
});
When a string is passed as an origin, it will be parsed as a URL and the
origin will be derived. For instance, the origin for the HTTP URL
'https://example.org/foo/bar' is the ASCII string
'https://example.org'. An error will be thrown if either the given string
cannot be parsed as a URL or if a valid origin cannot be derived.
A URL object, or any object with an origin property, may be passed as
an origin, in which case the value of the origin property will be
used. The value of the origin property must be a properly serialized
ASCII origin.
Alternatively, the origins option may be used when creating a new HTTP/2
server using the http2.createSecureServer() method:
import { createSecureServer } from 'node:http2';
const options = getSecureOptionsSomehow();
options.origins = ['https://example.com', 'https://example.org'];
const server = createSecureServer(options);
server.on('stream', (stream) => {
stream.respond();
stream.end('ok');
});
const http2 = require('node:http2');
const options = getSecureOptionsSomehow();
options.origins = ['https://example.com', 'https://example.org'];
const server = http2.createSecureServer(options);
server.on('stream', (stream) => {
stream.respond();
stream.end('ok');
});
Class: ClientHttp2Session
<!-- YAML added: v8.4.0 -->
- Extends: {Http2Session}
Event: 'altsvc'
<!-- YAML added: v9.4.0 -->
alt{string}origin{string}streamId{number}
The 'altsvc' event is emitted whenever an ALTSVC frame is received by
the client. The event is emitted with the ALTSVC value, origin, and stream
ID. If no origin is provided in the ALTSVC frame, origin will
be an empty string.
import { connect } from 'node:http2';
const client = connect('https://example.org');
client.on('altsvc', (alt, origin, streamId) => {
console.log(alt);
console.log(origin);
console.log(streamId);
});
const http2 = require('node:http2');
const client = http2.connect('https://example.org');
client.on('altsvc', (alt, origin, streamId) => {
console.log(alt);
console.log(origin);
console.log(streamId);
});
Event: 'origin'
<!-- YAML added: v10.12.0 -->
origins{string[]}
The 'origin' event is emitted whenever an ORIGIN frame is received by
the client. The event is emitted with an array of origin strings. The
http2session.originSet will be updated to include the received
origins.
import { connect } from 'node:http2';
const client = connect('https://example.org');
client.on('origin', (origins) => {
for (let n = 0; n < origins.length; n++)
console.log(origins[n]);
});
const http2 = require('node:http2');
const client = http2.connect('https://example.org');
client.on('origin', (origins) => {
for (let n = 0; n < origins.length; n++)
console.log(origins[n]);
});
The 'origin' event is only emitted when using a secure TLS connection.
clienthttp2session.request(headers[, options])
<!-- YAML
added: v8.4.0
changes:
- version: v24.2.0
pr-url: https://github.com/nodejs/node/pull/58293
description: The weight option is now ignored, setting it will trigger a
runtime warning.
- version:
- v24.2.0
- v22.17.0
- v20.19.6
pr-url: https://github.com/nodejs/node/pull/58313
description: Following the deprecation of priority signaling as of RFC 9113,
weight option is deprecated.
- version:
- v24.0.0
- v22.17.0
pr-url: https://github.com/nodejs/node/pull/57917
description: Allow passing headers in raw array format.
-->
-
headers{HTTP/2 Headers Object|HTTP/2 Raw Headers} -
options{Object} endStream{boolean}trueif theHttp2Streamwritable side should be closed initially, such as when sending aGETrequest that should not expect a payload body.exclusive{boolean} Whentrueandparentidentifies a parent Stream, the created stream is made the sole direct dependency of the parent, with all other existing dependents made a dependent of the newly created stream. Default:false.parent{number} Specifies the numeric identifier of a stream the newly created stream is dependent on.waitForTrailers{boolean} Whentrue, theHttp2Streamwill emit the'wantTrailers'event after the finalDATAframe has been sent.-
signal{AbortSignal} An AbortSignal that may be used to abort an ongoing request. -
Returns: {ClientHttp2Stream}
For HTTP/2 Client Http2Session instances only, the http2session.request()
creates and returns an Http2Stream instance that can be used to send an
HTTP/2 request to the connected server.
When a ClientHttp2Session is first created, the socket may not yet be
connected. If clienthttp2session.request() is called during this time, the
actual request will be deferred until the socket is ready to go.
If the session becomes unavailable before the request can be created, the
returned stream will emit ERR_HTTP2_GOAWAY_SESSION or
ERR_HTTP2_INVALID_SESSION asynchronously.
This method is only available if http2session.type is equal to
http2.constants.NGHTTP2_SESSION_CLIENT.
import { connect, constants } from 'node:http2';
const clientSession = connect('https://localhost:1234');
const {
HTTP2_HEADER_PATH,
HTTP2_HEADER_STATUS,
} = constants;
const req = clientSession.request({ [HTTP2_HEADER_PATH]: '/' });
req.on('response', (headers) => {
console.log(headers[HTTP2_HEADER_STATUS]);
req.on('data', (chunk) => { /* .. */ });
req.on('end', () => { /* .. */ });
});
const http2 = require('node:http2');
const clientSession = http2.connect('https://localhost:1234');
const {
HTTP2_HEADER_PATH,
HTTP2_HEADER_STATUS,
} = http2.constants;
const req = clientSession.request({ [HTTP2_HEADER_PATH]: '/' });
req.on('response', (headers) => {
console.log(headers[HTTP2_HEADER_STATUS]);
req.on('data', (chunk) => { /* .. */ });
req.on('end', () => { /* .. */ });
});
When the options.waitForTrailers option is set, the 'wantTrailers' event
is emitted immediately after queuing the last chunk of payload data to be sent.
The http2stream.sendTrailers() method can then be called to send trailing
headers to the peer.
When options.waitForTrailers is set, the Http2Stream will not automatically
close when the final DATA frame is transmitted. User code must call either
http2stream.sendTrailers() or http2stream.close() to close the
Http2Stream.
When options.signal is set with an AbortSignal and then abort on the
corresponding AbortController is called, the request will emit an 'error'
event with an AbortError error.
The :method and :path pseudo-headers are not specified within headers,
they respectively default to:
:method='GET':path=/
Class: Http2Stream
<!-- YAML added: v8.4.0 -->
- Extends: {stream.Duplex}
Each instance of the Http2Stream class represents a bidirectional HTTP/2
communications stream over an Http2Session instance. Any single Http2Session
may have up to 2<sup>31</sup>-1 Http2Stream instances over its lifetime.
User code will not construct Http2Stream instances directly. Rather, these
are created, managed, and provided to user code through the Http2Session
instance. On the server, Http2Stream instances are created either in response
to an incoming HTTP request (and handed off to user code via the 'stream'
event), or in response to a call to the http2stream.pushStream() method.
On the client, Http2Stream instances are created and returned when either the
http2session.request() method is called, or in response to an incoming
'push' event.
The Http2Stream class is a base for the [ServerHttp2Stream][] and
[ClientHttp2Stream][] classes, each of which is used specifically by either
the Server or Client side, respectively.
All Http2Stream instances are [Duplex][] streams. The Writable side of the
Duplex is used to send data to the connected peer, while the Readable side
is used to receive data sent by the connected peer.
The default text character encoding for an Http2Stream is UTF-8. When using an
Http2Stream to send text, use the 'content-type' header to set the character
encoding.
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 200,
});
Http2Stream Lifecycle
Creation
On the server side, instances of [ServerHttp2Stream][] are created either
when:
- A new HTTP/2
HEADERSframe with a previously unused stream ID is received; - The
http2stream.pushStream()method is called.
On the client side, instances of [ClientHttp2Stream][] are created when the
http2session.request() method is called.
On the client, the Http2Stream instance returned by http2session.request()
may not be immediately ready for use if the parent Http2Session has not yet
been fully established. In such cases, operations called on the Http2Stream
will be buffered until the 'ready' event is emitted. User code should rarely,
if ever, need to handle the 'ready' event directly. The ready status of an
Http2Stream can be determined by checking the value of http2stream.id. If
the value is undefined, the stream is not yet ready for use.
Destruction
All [Http2Stream][] instances are destroyed either when:
- An
RST_STREAMframe for the stream is received by the connected peer, and (for client streams only) pending data has been read. - The
http2stream.close()method is called, and (for client streams only) pending data has been read. - The
http2stream.destroy()orhttp2session.destroy()methods are called.
When an Http2Stream instance is destroyed, an attempt will be made to send an
RST_STREAM frame to the connected peer.
When the Http2Stream instance is destroyed, the 'close' event will
be emitted. Because Http2Stream is an instance of stream.Duplex, the
'end' event will also be emitted if the stream data is currently flowing.
The 'error' event may also be emitted if http2stream.destroy() was called
with an Error passed as the first argument.
After the Http2Stream has been destroyed, the http2stream.destroyed
property will be true and the http2stream.rstCode property will specify the
RST_STREAM error code. The Http2Stream instance is no longer usable once
destroyed.
Event: 'aborted'
<!-- YAML added: v8.4.0 -->
The 'aborted' event is emitted whenever a Http2Stream instance is
abnormally aborted in mid-communication.
Its listener does not expect any arguments.
The 'aborted' event will only be emitted if the Http2Stream writable side
has not been ended.
Event: 'close'
<!-- YAML added: v8.4.0 -->
The 'close' event is emitted when the Http2Stream is destroyed. Once
this event is emitted, the Http2Stream instance is no longer usable.
The HTTP/2 error code used when closing the stream can be retrieved using
the http2stream.rstCode property. If the code is any value other than
NGHTTP2_NO_ERROR (0), an 'error' event will have also been emitted.
Event: 'error'
<!-- YAML added: v8.4.0 -->
error{Error}
The 'error' event is emitted when an error occurs during the processing of
an Http2Stream.
Event: 'frameError'
<!-- YAML added: v8.4.0 -->
type{integer} The frame type.code{integer} The error code.id{integer} The stream id (or0if the frame isn't associated with a stream).
The 'frameError' event is emitted when an error occurs while attempting to
send a frame. When invoked, the handler function will receive an integer
argument identifying the frame type, and an integer argument identifying the
error code. The Http2Stream instance will be destroyed immediately after the
'frameError' event is emitted.
Event: 'ready'
<!-- YAML added: v8.4.0 -->
The 'ready' event is emitted when the Http2Stream has been opened, has
been assigned an id, and can be used. The listener does not expect any
arguments.
Event: 'timeout'
<!-- YAML added: v8.4.0 -->
The 'timeout' event is emitted after no activity is received for this
Http2Stream within the number of milliseconds set using
http2stream.setTimeout().
Its listener does not expect any arguments.
Event: 'trailers'
<!-- YAML added: v8.4.0 -->
headers{HTTP/2 Headers Object} An object describing the headersflags{number} The associated numeric flagsrawHeaders{HTTP/2 Raw Headers}
The 'trailers' event is emitted when a block of headers associated with
trailing header fields is received. The listener callback is passed the [HTTP/2 Headers Object][], flags associated
with the headers, and the headers in raw format (see [HTTP/2 Raw Headers][]).
This event might not be emitted if http2stream.end() is called
before trailers are received and the incoming data is not being read or
listened for.
stream.on('trailers', (headers, flags) => {
console.log(headers);
});
Event: 'wantTrailers'
<!-- YAML added: v10.0.0 -->
The 'wantTrailers' event is emitted when the Http2Stream has queued the
final DATA frame to be sent on a frame and the Http2Stream is ready to send
trailing headers. When initiating a request or response, the waitForTrailers
option must be set for this event to be emitted.
http2stream.aborted
<!-- YAML added: v8.4.0 -->
- Type: {boolean}
Set to true if the Http2Stream instance was aborted abnormally. When set,
the 'aborted' event will have been emitted.
http2stream.bufferSize
<!-- YAML added: - v11.2.0 - v10.16.0 -->
- Type: {number}
This property shows the number of characters currently buffered to be written.
See [net.Socket.bufferSize][] for details.
http2stream.close(code[, callback])
<!-- YAML
added: v8.4.0
changes:
- version: v18.0.0
pr-url: https://github.com/nodejs/node/pull/41678
description: Passing an invalid callback to the callback argument
now throws ERR_INVALID_ARG_TYPE instead of
ERR_INVALID_CALLBACK.
-->
code{number} Unsigned 32-bit integer identifying the error code. Default:http2.constants.NGHTTP2_NO_ERROR(0x00).callback{Function} An optional function registered to listen for the'close'event.
Closes the Http2Stream instance by sending an RST_STREAM frame to the
connected HTTP/2 peer.
http2stream.closed
<!-- YAML added: v9.4.0 -->
- Type: {boolean}
Set to true if the Http2Stream instance has been closed.
http2stream.destroyed
<!-- YAML added: v8.4.0 -->
- Type: {boolean}
Set to true if the Http2Stream instance has been destroyed and is no longer
usable.
http2stream.endAfterHeaders
<!-- YAML added: v10.11.0 -->
- Type: {boolean}
Set to true if the END_STREAM flag was set in the request or response
HEADERS frame received, indicating that no additional data should be received
and the readable side of the Http2Stream will be closed.
http2stream.id
<!-- YAML added: v8.4.0 -->
- Type: {number|undefined}
The numeric stream identifier of this Http2Stream instance. Set to undefined
if the stream identifier has not yet been assigned.
http2stream.pending
<!-- YAML added: v9.4.0 -->
- Type: {boolean}
Set to true if the Http2Stream instance has not yet been assigned a
numeric stream identifier.
http2stream.priority(options)
<!-- YAML added: v8.4.0 deprecated: - v24.2.0 - v22.17.0 - v20.19.6 changes: - version: v24.2.0 pr-url: https://github.com/nodejs/node/pull/58293 description: This method no longer sets the priority of the stream. Using it now triggers a runtime warning. -->
Stability: 0 - Deprecated: support for priority signaling has been deprecated in the [RFC 9113][] and is no longer supported in Node.js.
Empty method, only there to maintain some backward compatibility.
http2stream.rstCode
<!-- YAML added: v8.4.0 -->
- Type: {number}
Set to the RST_STREAM [error code][] reported when the Http2Stream is
destroyed after either receiving an RST_STREAM frame from the connected peer,
calling http2stream.close(), or http2stream.destroy(). Will be
undefined if the Http2Stream has not been closed.
http2stream.sentHeaders
<!-- YAML added: v9.5.0 -->
- Type: {HTTP/2 Headers Object}
An object containing the outbound headers sent for this Http2Stream.
http2stream.sentInfoHeaders
<!-- YAML added: v9.5.0 -->
- Type: {HTTP/2 Headers Object[]}
An array of objects containing the outbound informational (additional) headers
sent for this Http2Stream.
http2stream.sentTrailers
<!-- YAML added: v9.5.0 -->
- Type: {HTTP/2 Headers Object}
An object containing the outbound trailers sent for this HttpStream.
http2stream.session
<!-- YAML added: v8.4.0 -->
- Type: {Http2Session}
A reference to the Http2Session instance that owns this Http2Stream. The
value will be undefined after the Http2Stream instance is destroyed.
http2stream.setTimeout(msecs, callback)
<!-- YAML
added: v8.4.0
changes:
- version: v18.0.0
pr-url: https://github.com/nodejs/node/pull/41678
description: Passing an invalid callback to the callback argument
now throws ERR_INVALID_ARG_TYPE instead of
(本文超出站点单文档体积上限,已截断)
