Node.js http2.connect() Method Last Updated : 18 Nov, 2020 Comments Improve Suggest changes Like Article Like Report The http2.connect() is an inbuilt application programming interface of class http2 within the http2 module which is used to return a ClientHttp2Session instance. Syntax: const http2.connect(authority[, options][, listener]) Parameters: This method takes the following argument as a parameter: authority: It is the URL representing a remote HTTP/2 server to connect to.options: It can be maxDeflateDynamicTableSize, maxSettings, maxSessionMemory, etc option can be used according to need.listener: It is the one time listener of the 'connect' event. Return Value: This method returns the object of the ClientHttp2Session instance. How to generate a Private key and Public certificate? 1. Filename: Private key Open notepad and copy-paste the following key and save the file as private-key.pem -----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQC38R9wXcUbhOd44FavgmE5R3K4JeYOHLnI7dUq1B8/Gv7l3SOg JKef/m9gM1KvUx951mapXGtcWgwB08J3vUE2YOZ4tWJArrVZES0BI/RmFAyhQFP5 HcWl3LSM9LRihP98F33oIkKaCxA5LxOrkgpV4HrUzIKTABDYah7RPex1WQIDAQAB AoGBAIXR71xxa9gUfc5L7+TqBs+EMmrUb6Vusp8CoGXzQvRHMJCMrMFySV0131Nu o0YYRDsAh1nJefYLMNcXd1BjqI+qY8IeRsxaY+9CB2KKGVVDO2uLdurdC2ZdlWXT Vwr3dDoyR0trnXJMmH2ijTeO6bush8HuXxvxJBjvEllM5QYxAkEA3jwny9JP+RFu 0rkqPBe/wi5pXpPl7PUtdNAGrh6S5958wUoR4f9bvwmTBv1nQzExKWu4EIp+7vjJ fBeRZhnBvQJBANPjjge8418PS9zAFyKlITq6cxmM4gOWeveQZwXVNvav0NH+OKdQ sZnnDiG26JWmnD/B8Audu97LcxjxcWI8Jc0CQEYA5PhLU229lA9EzI0JXhoozIBC TlcKFDuLm88VSmlHqDyqvF9YNOpEdc/p2rFLuZS2ndB4D+vu6mjwc5iZ3HECQCxy GBHRclQ3Ti9w76lpv+2kvI4IekRMZWDWnnWfwta+DGxwCgw2pfpleBZkWqdBepb5 JFQbcxQJ0wvRYXo8qaUCQQCgTvWswBj6OTP7LTvBlU1teAN2Lnrk/N5AYHZIXW6m nUG9lYvH7DztWDTioXMrruPF7bdXfZOVJD8t0I4OUzvC -----END RSA PRIVATE KEY----- 2. Filename: Public certificate Open notepad and copy-paste the following key and save the file as public-cert.pem -----BEGIN CERTIFICATE----- MIICfzCCAegCCQDxxeXw914Y2DANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMC SU4xEzARBgNVBAgMCldlc3RiZW5nYWwxEDAOBgNVBAcMB0tvbGthdGExFDASBgNV BAoMC1BhbmNvLCBJbmMuMRUwEwYDVQQDDAxSb2hpdCBQcmFzYWQxIDAeBgkqhkiG 9w0BCQEWEXJvZm9mb2ZAZ21haWwuY29tMB4XDTIwMDkwOTA1NTExN1oXDTIwMTAw OTA1NTExN1owgYMxCzAJBgNVBAYTAklOMRMwEQYDVQQIDApXZXN0YmVuZ2FsMRAw DgYDVQQHDAdLb2xrYXRhMRQwEgYDVQQKDAtQYW5jbywgSW5jLjEVMBMGA1UEAwwM Um9oaXQgUHJhc2FkMSAwHgYJKoZIhvcNAQkBFhFyb2ZvZm9mQGdtYWlsLmNvbTCB nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAt/EfcF3FG4TneOBWr4JhOUdyuCXm Dhy5yO3VKtQfPxr+5d0joCSnn/5vYDNSr1MfedZmqVxrXFoMAdPCd71BNmDmeLVi QK61WREtASP0ZhQMoUBT+R3Fpdy0jPS0YoT/fBd96CJCmgsQOS8Tq5IKVeB61MyC kwAQ2Goe0T3sdVkCAwEAATANBgkqhkiG9w0BAQsFAAOBgQATe6ixdAjoV7BSHgRX bXM2+IZLq8kq3s7ck0EZrRVhsivutcaZwDXRCCinB+OlPedbzXwNZGvVX0nwPYHG BfiXwdiuZeVJ88ni6Fm6RhoPtu2QF1UExfBvSXuMBgR+evp+e3QadNpGx6Ppl1aC hWF6W2H9+MAlU7yvtmCQQuZmfQ== -----END CERTIFICATE----- Example 1: Filename: index.js JavaScript // Node.js program to demonstrate the // http2.connect() method 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"); }) }); Run the index.js file using the following command: node index.js Output: status : 200 Received: hello Received: world client closed server closed Example 2: Filename: index.js JavaScript // Node.js program to demonstrate the // http2.connect() method 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"); }) }); Run the index.js file using the following command: node index.js Output: Received: world client closed server closed Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_http2_connect_authority_options_listener Comment More infoAdvertise with us Next Article Node.js http2.connect() Method rohitprasad3 Follow Improve Article Tags : Web Technologies Node.js Node.js-Methods Similar Reads Http2Session EventNode.js Http2Session timeout EventThe 'timeout' Event in http2 server is emitted  if there is no activity on the Http2Session after the configured number of milliseconds. Syntax: Event: 'timeout' Parameters: This event does not accept any argument as a parameter. Return Value: This event returns nothing but a callback function. How 3 min read Node.js Http2Session Close EventThe 'close' Event in the http2 server is emitted when the Http2Session has been destroyed. Syntax: Event: 'close' Parameters: This event does not accept any argument as a parameter. Return Value: This event returns nothing but a callback function. How to generate a Private key and Public certificate 3 min read Http2Session MethodsNode.js http2session.state MethodThe http2session.state is an inbuilt application programming interface of class http2session within http2 module which is used to return miscellaneous information about the current state of the Http2Session. Syntax: const http2session.state Parameters: This method does not accept any parameter. Retu 3 min read Node.js http2session.socket MethodThe http2session.socket is an inbuilt application programming interface of class http2session within http2 module which is used to return a Proxy object that acts as a net.Socket. Syntax: const http2session.socket Parameters: This method does not accept any parameter. Return Value: This method retur 3 min read Node.js http2session.remoteSettings MethodThe http2session.remoteSettings is an inbuilt application programming interface of class http2session within http2 module which is used to get a prototype-less object describing the current remote settings of this Http2Session. Syntax: const http2session.remoteSettings Parameters: This method does n 3 min read Node.js http2session.destroyed MethodThe http2session.destroyed is an inbuilt application programming interface of class http2session within http2 module which is used to check if the session is destroyed or not. Syntax: const http2session.destroyed Parameters: This method does not accept any parameter. Return Value: This method return 3 min read Node.js http2session.type MethodThe http2session.type is an inbuilt application programming interface of class http2session within http2 module which is used to return the type of session instance used in the peer process. Syntax: const http2session.type Parameters: This method does not takes any argument as a parameter. Return Va 3 min read Node.js http2session.encrypted MethodThe http2session.encrypted  is  an inbuilt application programming interface of class http2session within http2 module which is used to check if the Http2Session is connected with a TLSSocket or not. Syntax: const http2session.encrypted Parameters: This method does not accept any parameter. Return V 3 min read Node.js http2session.localSettings MethodThe http2session.localSettings  is  an inbuilt application programming interface of class http2session within http2 module which is used to get a prototype-less object describing the current local settings of this Http2Session. Syntax: const http2session.localSettings Parameters: This method does no 3 min read Node.js http2session.pendingSettingsAck MethodThe http2session.pendingSettingsAck  is  an inbuilt application programming interface of class http2session within http2 module which is used to indicate whether the Http2Session is currently waiting for acknowledgment of a sent SETTINGS frame. Syntax: const http2session.pendingSettingsAck Parameter 3 min read Node.js http2session.close() MethodThe http2session.close() is an inbuilt application programming interface of class http2session within http2 module which is used to close this particular session. Syntax: const http2session.close([callback]) Parameters: This method has one optional parameter ie. callback function which is executed w 3 min read Node.js http2session.closed MethodThe http2session.closed is an inbuilt application programming interface of class http2session within http2 module which is used to check if this Http2Session instance has been closed or not. Syntax: const http2session.closed Parameters: This method does not accept any parameter. Return Value: This m 3 min read Node.js http2session.alpnProtocol MethodThe http2session.alpnProtocol is an inbuilt application programming interface of class http2session within http2 module which is used to get alp protocol property associated with this http2session object. Syntax: const http2session.alpnProtocol Parameters: This method does not accept any parameter. 3 min read Node.js http2session.unref() MethodThe http2session.unref() is an inbuilt application programming interface of class http2session within http2 module which is used to return instance of the socket object associated with this session object. Syntax: const http2session.unref() Parameters: This method does not accept any parameter. Retu 3 min read Node.js http2session.destroy() MethodThe http2session.destroy() is an inbuilt application programming interface of class http2session within http2 module which is used to destroy this particular session. Syntax: const http2session.destroy([error][, code]) Parameters: This method takes the following two optional arguments as a parameter 3 min read Node.js http2session.connecting MethodThe http2session.connecting is an inbuilt application programming interface of class http2session within http2 module which is used to check if this Http2Session instance is still connecting or not. Syntax: const http2session.connecting Parameters: This method does not accept any parameter. Return V 3 min read Node.js http2session.ping() MethodThe http2session.ping() is an inbuilt application programming interface of class http2session within http2 module which is used to sends a PING frame to the connected HTTP/2 peer. Syntax: const http2session.ping([payload, ]callback) Parameters: This method takes the optional ping payload buffer as a 3 min read Node.js http2session.setTimeout() MethodThe http2session.setTimeout() is an inbuilt application programming interface of class http2session within http2 module which is used to set the duration for a time after which a particular action will take place. Syntax: const http2session.setTimeout(msecs, callback) Parameters: This method takes t 3 min read ClientHttp2Session MethodsNode.js clienthttp2session.request() MethodThe clienthttp2session.request() is an inbuilt application programming interface of class ClientHttp2Session within http2 module which is used to return the object of ClientHttp2Stream. Syntax: const clienthttp2session.request(headers[, options]) Parameters: This method takes the following argument 3 min read Http2Stream EventNode.js Http2Stream Timeout EventThe 'timeout' Event in the http2 server is emitted if there is no activity on the Http2Stream after the configured number of milliseconds. Syntax: Event: 'timeout' Parameters: This event does not accept any argument as a parameter. Return Value: This event returns nothing but a callback function. Ho 3 min read Node.js Http2Stream close EventThe 'close' Event in http2 server is emitted when the Http2Stream has been destroyed. Syntax: Event: 'close' Parameters: This event does not accept any argument as a parameter. Return Value: This event returns nothing but a callback function. How to generate private key and public certificate? Priva 3 min read Http2Stream MethodsNode.js http2stream.state MethodThe http2stream.state is an inbuilt application programming interface of class Http2Stream within http2 module which is used to get the miscellaneous information about the current state of the Http2Stream. Syntax: const http2stream.state Parameters: This method does not accept any parameter. Return 3 min read Node.js http2stream.priority() MethodThe http2stream.priority() is an inbuilt application programming interface of class Http2Stream within http2 module which is used to update the priority for this Http2Stream instance. Syntax: const http2stream.priority(options) Parameters: This method accepts the following options parameter: exclusi 3 min read Node.js http2stream.setTimeout() MethodThe http2stream.setTimeout() method is an inbuilt application programming interface of class Http2Stream within http2 module which is used to set the duration for a time after which a particular action will take place. Syntax: const http2stream.setTimeout(msecs, callback) Parameters: This method tak 3 min read Node.js http2stream.id MethodThe http2stream.id is an inbuilt application programming interface of class Http2Stream within http2 module which is used to return numeric stream identifier of this Http2Stream. Syntax: const http2stream.id Parameters: This method does not accept any parameter. Return Value: This method returns num 3 min read Node.js http2stream.closed MethodThe http2stream.closed is an inbuilt application programming interface of class Http2Stream within http2 module which is used to return true if the stream is closed other wise false. Syntax: http2stream.closed Parameters: This method does not accept any parameter. Return Value: This method returns t 3 min read Node.js http2stream.endAfterHeaders MethodThe http2stream.endAfterHeaders is an inbuilt application programming interface of class Http2Stream within http2 module which is used to return true if END_STREAM flag was set in the request or response HEADERS frame received. Syntax: http2stream.endAfterHeaders Parameters: This method does not acc 3 min read Node.js http2stream.pending MethodThe http2stream.pending is an inbuilt application programming interface of class Http2Stream within http2 module which is used to check if Http2Stream instance has been assigned a numeric stream identifier or not. Syntax: const http2stream.pending Parameters: This method does not accept any paramete 3 min read Node.js http2stream.destroyed MethodThe http2stream.destroyed is an inbuilt application programming interface of class Http2Stream within http2 module which is used to return true if the stream is destroyed other wise false. Syntax: http2stream.destroyed Parameters: This method does not accept any parameter. Return Value: This method 3 min read Node.js http2stream.session MethodThe http2stream.session is an inbuilt application programming interface of class Http2Stream within http2 module which is used to get an reference to the Http2Session instance that owns this Http2Stream. Syntax: const http2stream.session Parameters: This method does not accept any parameter. Return 3 min read Node.js http2stream.close() MethodThe http2stream.close() is an inbuilt application programming interface of class Http2Stream within http2 module which is used to close the Http2Stream instance. Syntax: http2stream.close(code[, callback]) Parameters: This method has following two parameters: code: It is the unsigned integer of 32-b 3 min read Node.js http2stream.rstCode MethodThe http2stream.rstCode is an inbuilt application programming interface of class Http2Stream within http2 module which is used to Set to the RST_STREAM error code reported when the Http2Stream is destroyed. Syntax: const http2stream.rstCode Parameters: This method does not accept any parameter. Retu 3 min read Node.js http2stream.sentHeaders MethodThe http2stream.sentHeaders is an inbuilt application programming interface of class Http2Stream within http2 module which is used to get the object containing the outbound headers sent to this Http2Stream. Syntax: const http2stream.sentHeaders Parameters: This method does not accept any parameter. 3 min read Node.js http2stream.sentInfoHeaders MethodThe http2stream.sentInfoHeaders is an inbuilt application programming interface of class Http2Stream within http2 module which is used to get an object containing the outbound informational (additional) headers sent to this Http2Stream. Syntax: http2stream.sentInfoHeaders Parameters: This method doe 3 min read ServerHttp2Stream MethodNode.js http2stream.additionalHeaders() MethodThe http2stream.additionalHeaders() is an inbuilt application programming interface of class Http2Stream within http2 module which is used to send an additional informational HEADERS frame to the connected HTTP/2 peer. Syntax: const http2stream.additionalHeaders(headers) Parameters: This method acce 3 min read Node.js http2stream.headersSent MethodThe http2stream.headersSent is an inbuilt application programming interface of class Http2Stream within http2 module which is used to check if the header were sent or not. Syntax: const http2stream.headersSent Parameters: This method does not accept any parameter. Return Value: This method returns t 3 min read Node.js http2stream.pushAllowed MethodThe http2stream.pushAllowed is an inbuilt application programming interface of class Http2Stream within http2 module which is used to check if the header were sent or not. Syntax: const http2stream.pushAllowed Parameters: This method does not accept any parameter. Return Value: This method returns t 3 min read Node.js http2stream.respond() MethodThe http2stream.respond()  is  an inbuilt application programming interface of class Http2Stream within http2 module which is used to sent the response header of the particular stream to its client. Syntax: const http2stream.respond([headers[, options]]) Parameters: This method takes the response he 3 min read http2.Http2ServerRequest PropertiesNode.js Http2ServerRequest.aborted PropertyThe Http2ServerRequest.aborted  is  an inbuilt application programming interface of class Http2ServerRequest within http2 module which is used to check if the particular server request is aborted or not. Syntax: const request.aborted Parameters: This method does not take any arguments as a parameter 3 min read http2.Http2ServerRequest EventNode.js Http2ServerRequest close EventThe 'close' Event in the http2 server is emitted when an underlying Http2Stream was closed. Syntax: Event: 'close' Parameters: This event does not accept any argument as a parameter. Return Value: This event returns nothing but a callback function. How to generate a Private key and Public certificat 3 min read http2.Http2ServerRequest MethodNode.js Http2ServerRequest.url MethodThe Http2ServerRequest.url is an inbuilt application programming interface of class Http2ServerRequest within http2 module which is used to get the Request URL string. This contains only the URL that is present in the actual HTTP request. Syntax: const request.url Parameters: This method does not ac 3 min read Node.js Http2ServerRequest.httpVersion MethodThe Http2ServerRequest.httpVersion is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the HTTP version either associated with server or client. Syntax: const request.httpVersion Parameters: This method does not accept any argument 3 min read Node.js Http2ServerRequest.headers MethodThe Http2ServerRequest.headers is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the request/response headers object. Syntax: const request.headers Parameters: This method does not accept any argument as a parameter. Return Value 3 min read Node.js Http2ServerRequest.rawHeaders MethodThe Http2ServerRequest.rawHeaders is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the raw request/response headers to list exactly as they were received. Syntax: const request.rawHeaders Parameters: This method does not accept 3 min read Node.js Http2ServerRequest.destroy() MethodThe Http2ServerRequest.destroy() is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to destroy the request. Syntax: const request.destroy([error]) Parameters: This method accepts the error code if any error event is emitted. Return Value 3 min read Node.js Http2ServerRequest.authority MethodThe Http2ServerRequest.authority is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the string representation of the request authority pseudo-header field. Syntax: const request.authority Parameters: This method does not accept an 3 min read Node.js Http2ServerRequest.complete MethodThe Http2ServerRequest.complete is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to check whether this request has been completed, aborted, or destroyed or not. Syntax: const request.complete Parameters: This method does not accept an 4 min read Node.js Http2ServerRequest.method MethodThe Http2ServerRequest.method is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the string representation of the request method. Syntax: const request.method Parameters: This method does not accept any argument as a parameter. Re 3 min read Node.js Http2ServerRequest.scheme MethodThe Http2ServerRequest.scheme is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the request scheme pseudo-header field indicating the scheme portion of the target URL. Syntax: const request.scheme Parameters: This method does not 3 min read Node.js Http2ServerRequest.rawTrailers MethodThe Http2ServerRequest.rawTrailers is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get the raw request/response trailer keys and values exactly as they were received. Syntax: const request.rawTrailers Parameters: This method does n 3 min read Node.js Http2ServerRequest.socket MethodThe Http2ServerRequest.socket is an inbuilt application programming interface of class Http2ServerRequest within the http2 module which is used to get a Proxy object that acts as a net.Socket (or tls.TLSSocket). Syntax: const request.socket Parameters: This method does not accept any argument as a p 3 min read http2.Http2ServerResponse PropertiesNode.js Http2ServerResponse.headersSent PropertyThe Http2ServerResponse.headersSent is an inbuilt application programming interface of the class Http2ServerResponse within the http2 module which is used to check if headers were sent or not. Syntax: response.headersSentParameters: This property does not accept any arguments as a parameter. Return 3 min read Like