The HTTP2 module in Node.js provides support for the HTTP/2 protocol, enabling faster and more efficient communication between clients and servers.
- The HTTP2 module is a built-in module used to implement the HTTP/2 protocol in Node.js.
- It improves performance through features like multiplexing, header compression, and server push.
- It allows developers to create HTTP/2 servers and manage client–server communication efficiently.
// Node.js program to demonstrate the close event in HTTP2
const http2 = require('http2');
// Creating HTTP/2 server
const server = http2.createServer();
server.on('stream', (stream, headers) => {
// Send response headers
stream.respond({
':status': 200,
'content-type': 'text/plain'
});
stream.write('Hello ');
// Get Http2Session from stream
const session = stream.session;
// Get ALPN protocol used
const protocol = session.alpnProtocol;
stream.end("Session protocol: " + protocol);
// Listen for session close event
session.on('close', () => {
console.log("Session is closed");
});
// Close the session
session.close();
});
// Start server
server.listen(8000, () => {
console.log("Server running at http://localhost:8000");
});
// Creating HTTP/2 client
const client = http2.connect('http://localhost:8000');
const req = client.request({
':method': 'GET',
':path': '/'
});
// Handle response headers
req.on('response', (headers) => {
console.log("Status:", headers[':status']);
});
// Handle response data
req.on('data', (chunk) => {
console.log("Received:", chunk.toString());
});
// Close client after response
req.on('end', () => {
client.close(() => {
console.log("Client destroyed");
});
});
req.end();
Output:
status : 200
Received: hello
Received: local window size : 65535
client localSettings
server localSettings
Classes in the Node.js HTTP2 Module
1. Class: Http2Session
Represents an HTTP/2 session that manages communication between peers.
Events
- timeout: The timeout event in Http2Session occurs when there is no activity for a specified time.
- close: The close event in Http2Session is emitted when the session is destroyed.
Methods
- state(): The state() method of Http2Session returns the current session state.
- socket(): The socket() method of Http2Session returns the associated socket.
- remoteSettings(): The remoteSettings() method of Http2Session returns remote session settings.
- destroyed(): The destroyed() method of Http2Session checks if the session is destroyed.
- type(): The type() method of Http2Session returns the session type.
- encrypted(): The encrypted() method of Http2Session checks if the session uses TLS.
- localSettings(): The localSettings() method of Http2Session returns local settings.
- pendingSettingsAck(): The pendingSettingsAck() method of Http2Session checks for SETTINGS acknowledgment.
- close(): The close() method of Http2Session closes the session.
- closed(): The closed() method of Http2Session checks if the session is closed.
- alpnProtocol(): The alpnProtocol() method of Http2Session returns the ALPN protocol used.
- unref(): The unref() method of Http2Session returns the associated socket instance.
- destroy(): The destroy() method of Http2Session destroys the session.
- connecting(): The connecting() method of Http2Session checks if the session is connecting.
- ping(): The ping() method of Http2Session sends a PING frame.
- setTimeout(): The setTimeout() method of Http2Session sets a session timeout.
2. Class: ClientHttp2Session
ClientHttp2Session represents an HTTP/2 client session.
Methods
- request(): The request() method of ClientHttp2Session returns a ClientHttp2Stream object.
3. Class: Http2Stream
Http2Stream represents an HTTP/2 stream for request–response communication.
Events
- timeout: The timeout event in Http2Stream occurs when the stream is inactive.
- close: The close event in Http2Stream is emitted when the stream is destroyed.
Methods
- state(): The state() method of Http2Stream returns the stream state.
- priority(): The priority() method of Http2Stream updates the stream priority.
- setTimeout(): The setTimeout() method of Http2Stream sets a stream timeout.
- id(): The id() method of Http2Stream returns the stream identifier.
- closed(): The closed() method of Http2Stream checks if the stream is closed.
- endAfterHeaders(): The endAfterHeaders() method of Http2Stream checks if the END_STREAM flag is set.
- pending(): The pending() method of Http2Stream checks if the stream identifier is assigned.
- destroyed(): The destroyed() method of Http2Stream checks if the stream is destroyed.
- session(): The session() method of Http2Stream returns the associated Http2Session.
- close(): The close() method of Http2Stream closes the stream.
- rstCode(): The rstCode() method of Http2Stream returns the RST_STREAM error code.
- sentHeaders(): The sentHeaders() method of Http2Stream returns sent headers.
- sentInfoHeaders(): The sentInfoHeaders() method of Http2Stream returns informational headers.
4. Class: ServerHttp2Stream
ServerHttp2Stream represents an HTTP/2 stream on the server side.
Methods
- additionalHeaders(): The additionalHeaders() method of ServerHttp2Stream sends extra headers.
- headersSent(): The headersSent() method of ServerHttp2Stream checks if headers are sent.
- pushAllowed(): The pushAllowed() method of ServerHttp2Stream checks if server push is allowed.
- respond(): The respond() method of ServerHttp2Stream sends response headers.
5. Class: http2.Http2ServerRequest
Http2ServerRequest represents the incoming HTTP/2 request.
Properties
- aborted: The aborted property of Http2ServerRequest indicates if the request is aborted.
Events
- close: The close event in Http2ServerRequest occurs when the underlying stream closes.
Methods
- url(): The url() method of Http2ServerRequest returns the request URL.
- httpVersion(): The httpVersion() method of Http2ServerRequest returns the HTTP version.
- headers(): The headers() method of Http2ServerRequest returns request headers.
- rawHeaders(): The rawHeaders() method of Http2ServerRequest returns raw headers.
- destroy(): The destroy() method of Http2ServerRequest destroys the request.
- authority(): The authority() method of Http2ServerRequest returns the request authority.
- complete(): The complete() method of Http2ServerRequest checks request completion.
- method(): The method() method of Http2ServerRequest returns the request method.
- scheme(): The scheme() method of Http2ServerRequest returns the request scheme.
- rawTrailers(): The .rawTrailers() method of Http2ServerRequest returns trailer headers.
- socket(): The socket() method of Http2ServerRequest returns a proxy socket object.
6. Class: http2.Http2ServerResponse
Http2ServerResponse represents the outgoing HTTP/2 response.
Properties
- headersSent: The headersSent property of Http2ServerResponse indicates if headers were sent.
Events
- close: The close event in Http2ServerResponse occurs when the stream closes.
- finish: The finish event in Http2ServerResponse occurs when the response is fully sent.
Methods
- setHeader(): The setHeader() method of Http2ServerResponse sets a response header.
- setTimeout(): The setTimeout() method of Http2ServerResponse sets a response timeout.
- stream(): The stream() method of Http2ServerResponse returns the HTTP stream.
- statusMessage(): The statusMessage() method of Http2ServerResponse returns an empty string in HTTP/2.
- writableEnded(): The writableEnded() method of Http2ServerResponse checks if response.end() was called.
- write(): The write() method of Http2ServerResponse sends response data.
- writeHead(): The writeHead() method of Http2ServerResponse sends response headers.
- sendDate(): The sendDate() method of Http2ServerResponse checks automatic Date header generation.
- socket(): The socket() method of Http2ServerResponse returns a proxy socket.
- statusCode(): The statusCode() method of Http2ServerResponse returns the response status code.
- getHeaderNames(): The getHeaderNames() method of Http2ServerResponse returns header names.
- removeHeader(): The removeHeader() method of Http2ServerResponse removes a header.
- hasHeader(): The hasHeader() method of Http2ServerResponse checks if a header exists.
- getHeaders(): The getHeaders() method of Http2ServerResponse returns outgoing headers.
- end(): The end() method of Http2ServerResponse completes the response.
- finished(): The finished() method of Http2ServerResponse checks response completion.
- getHeader(): The getHeader() method of Http2ServerResponse returns a queued header.
http2.constants
The http2.constants object provides predefined constants used in HTTP/2 operations.
Properties
- constants: The constants property of http2.constants provides HTTP/2 error codes.
HTTP2 Module Methods
The Node.js HTTP2 module provides several classes that manage HTTP/2 sessions, streams, requests, and responses for efficient client–server communication.
- getPackedSettings(): The getPackedSettings() method of http2 module returns serialized HTTP/2 settings.
- connect(): The connect() method of http2 module creates a ClientHttp2Session.
- getUnpackedSettings(): The getUnpackedSettings() method of http2 module returns deserialized settings.
- getDefaultSettings(): The getDefaultSettings() method of http2 module returns default session settings.
- createServer(): The createServer() method of http2 module creates an HTTP/2 server.
- prompt.get(): The prompt.get() method performs input/output operations.
- aborted(): The aborted() method checks if an aborted event is emitted.
- bufferSize(): The bufferSize() method returns the number of buffered characters.