The http2.connect() method in Node.js creates a client-side HTTP/2 session with a server. It returns an Http2Session object that can be used to send HTTP/2 requests and receive responses.
- It is a method of the built-in
http2module. - It establishes a connection to an HTTP/2 server.
- It accepts the server URL and optional configuration settings.
Syntax:
http2.connect(authority[, options][, listener])Where
- authority: It is the URL representing a remote HTTP/2 server to connect to.
- options (optional): It can be maxDeflateDynamicTableSize, maxSettings, maxSessionMemory, etc option can be used according to need.
- listener (optional): It is the one time listener of the 'connect' event.
Return Value: This method returns an Http2Session object that represents the client-side HTTP/2 connection.
Example 1:
const http2 = require('http2');
const fs = require('fs');
// Private key and public certificate for access
const options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('public-cert.pem'),
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options);
server.on('stream', (stream, requestHeaders) => {
stream.respond({
':status': 200,
'content-type': 'text/plain'
});
stream.write('hello ');
stream.end('world');
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server closed");
})
});
server.listen(8000);
// Creating and initializing client
// by using http2.connect() method
const client = http2.connect(
'http://localhost:8000');
const req = client.request({
':method': 'GET', ':path': '/' });
req.on('response', (responseHeaders) => {
console.log("status : "
+ responseHeaders[":status"]);
});
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(() => {
console.log("client closed");
})
});
Output:
status : 200
Received: hello
Received: world
client closed
server closed
Example 2:
const http2 = require('http2');
const fs = require('fs');
// Private key and public certificate for access
const options = {
key: fs.readFileSync('private-key.pem'),
cert: fs.readFileSync('public-cert.pem'),
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options);
server.on('stream', (stream, requestHeaders) => {
stream.end('world');
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server closed");
})
});
server.listen(8000);
// Creating and initializing client
// by using http2.connect() method
const client = http2.connect(
'http://localhost:8000');
const req = client.request({
':method': 'GET', ':path': '/' });
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(() => {
console.log("client closed");
})
});
Output:
Received: world
client closed
server closed
Use Cases of http2.connect() Method:
- It is used to create client-side HTTP/2 connections.
- It is used to send requests to HTTP/2 servers.
- It is useful for building high-performance network applications.
- It is commonly used to communicate with secure HTTP/2 APIs.