Node.js HTTP2 Complete Reference
HTTP2 module is used to provide an implementation of the HTTP2 protocol. It reduces overheads by compressing the server request headers.
Example:
Javascript
// Node.js program to demonstrate the // close event 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 ' ); // Getting session object // by using session method const http2session = stream.session; // Getting alpnProtocol of this session // by using alpnProtocol method const alpnProtocol = http2session.alpnProtocol; stream.end( "session protocol : " + alpnProtocol); http2session.close(); // Handling 'close' event http2session.on( 'close' ,() => { console.log( "session is closed" ); }) // Stopping the server // by using the close() method server.close(() => { console.log( "server destroyed" ); }) }); server.listen(8000); // Creating and initializing client // by using tls.connect() method const client = http2.connect( 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 destroyed" ); }) }); |
Output:
status : 200 Received: hello Received: local window size : 65535 client localSettings server localSettings
The Complete List of HTTP2 module are listed below:
Class: Http2Session
Class: Http2Session Event |
Description |
---|---|
timeout | There is no activity on the Http2Session after the configured number of milliseconds. |
NClose | The ‘close’ Event in the http2 server is emitted when the Http2Session has been destroyed. |
Class: Http2Session Methods |
Description |
---|---|
state() | Return miscellaneous information about the current state of the Http2Session. |
socket() | It is an inbuilt application programming interface of class http2session |
remoteSettings() | Get a prototype-less object describing the current remote settings of this Http2Session. |
destroyed() | Check if the session is destroyed or not. |
type() | Return the type of session instance used in the peer |
encrypted() | Check if the Http2Session is connected with a TLSSocket or not. |
localSettings() | Get the prototype-less object describing the current local settings of this Http2Session. |
pendingSettingsAck() | Indicate whether the Http2Session is currently waiting for an acknowledgment of a sent SETTINGS frame. |
close() | close this particular session. |
closed() | Check if this Http2Session instance has been closed or not. |
alpnProtocol() | Get the alp protocol property associated with this http2session object. |
unref() | Return instance of the socket object associated with this session object. |
destroy() | Destroy this particular session. |
connecting() | Check if this Http2Session instance is still connecting or not. |
ping() | Sends a PING frame to the connected HTTP/2 peer. |
setTimeout() | Set the duration for a time after which a particular action will take place. |
Class: ClientHttp2Session
Class: ClientHttp2Session Methods |
Description |
---|---|
request() | Return the object of ClientHttp2Stream. |
Class: Http2Stream
Class: Http2Stream Event |
Description |
---|---|
Timeout | There is no activity on the Http2Stream after the configured number of milliseconds. |
close | The ‘close’ Event in http2 server is emitted when the Http2Stream has been destroyed. |
Class: Http2Stream Methods |
Description |
---|---|
state() | Get miscellaneous information about the current state of the Http2Stream. |
priority() | Update the priority for this Http2Stream instance. |
setTimeout() | Set the duration for a time after which a particular action will take place. |
id() | Return numeric stream identifier of this Http2Stream. |
closed() | Return true if the stream is closed other wise false. |
endAfterHeaders() | Return true if END_STREAM flag was set in the request or response HEADERS frame received. |
pending() | Check if Http2Stream instance has been assigned a numeric stream identifier or not. |
destroyed() | Return true if the stream is destroyed otherwise false. |
session() | Get a reference to the Http2Session instance that owns this Http2Stream. |
close() | Close the Http2Stream instance. |
rstCode() | Set to the RST_STREAM error code reported when the Http2Stream is destroyed. |
sentHeaders() | Get the object containing the outbound headers sent to this Http2Stream. |
sentInfoHeaders() | Get the object containing the outbound headers sent to this Http2Stream. |
Class: ServerHttp2Stream
Class: ServerHttp2Stream Method |
Description |
---|---|
additionalHeaders() | Send an additional informational HEADERS frame to the connected HTTP/2 peer. |
headersSent() | Check if the header were sent or not. |
pushAllowed() | Check if the header were sent or not |
respond() | This is used to sent the response header of the particular stream to its client. |
Class: http2.Http2ServerRequest
Class: http2.Http2ServerRequest Properties |
Description |
---|---|
aborted | Check if the particular server request is aborted or not. |
Class: http2.Http2ServerRequest Event |
Description |
---|---|
close | The ‘close’ Event in the http2 server is emitted when an underlying Http2Stream was closed. |
Class: http2.Http2ServerRequest Method |
Description |
---|---|
url() | Get the Request URL string. This contains only the URL that is present in the actual HTTP request. |
httpVersion() | Get the HTTP version either associated with server or client. |
headers() | Get the request/response headers object. |
rawHeaders() | Get the raw request/response headers to list exactly as they were received. |
destroy() | This is used to destroy the request. |
authority() | Get the string representation of the request authority pseudo-header field. |
complete() | Check whether this request has been completed, aborted, or destroyed or not. |
method() | Get the string representation of the request . |
scheme() | Get the request scheme pseudo-header field indicating the scheme portion of the target URL. |
.rawTrailers() | Get the raw request/response trailer keys and values exactly as they were received. |
socket() | Get a Proxy object that acts as a net.Socket (or tls.TLSSocket). |
Class: http2.Http2ServerResponse
Class: http2.Http2ServerResponse Properties |
Description |
---|---|
headersSent | Check if headers were sent or not. |
Class: http2.Http2ServerResponse Event |
Description |
---|---|
close | The ‘close’ Event in the http2 server is emitted when an underlying Http2Stream was closed. |
finish | The ‘finish’ event in the http2 server is emitted when the last segment of the response headers and body have been sent. |
Class: http2.Http2ServerResponse Method |
Description |
---|---|
setHeader() | Set the header name and the particular value of that data header. |
setTimeout() | Set the duration for a time after which a particular action will take place. |
stream() | Get the object of HTTP stream backing this response. |
statusMessage() | Return an empty string because it is not supported by HTTP/2 (RFC 7540 8.1.2.4). |
writableEnded() | Check if the response.end() has been called or not. |
write() | Send the chunk of the response body to the client. |
writeHead() | Send the response header to the request. |
sendDate() | Check if the Date header is automatically generated and sent in the response or not. |
socket() | Get a Proxy object that acts as a net.Socket. |
statusCode() | Show the different status of data header during the transaction. |
getHeaderNames() | Return an array containing the unique names of the current outgoing headers. |
removeHeader() | Remove a header that has been queued for implicit sending. |
hasHeader() | Check if the header identified by name is currently set in the outgoing headers or not. |
getHeaders() | Return a shallow copy of the current outgoing headers. |
end() | Send the signal that all the response header and body have been sent. |
finished() | Check whether the response has completed or not. |
getHeader() | Read out a header that has already been queued but not sent to the client. |
http2.constants:
http2.constants Properties |
Description |
---|---|
constants | Provide the error codes for particular errors. |
http2 Methods:
http2 Methods |
Description |
---|---|
getPackedSettings() | Provide an object containing the serialized representation of the given HTTP/2 |
connect() | Return a ClientHttp2Session instance. |
getUnpackedSettings() | Provide an HTTP/2 Settings Object containing the deserialized settings |
getDefaultSettings() | Provide an object containing the default settings for an Http2Session instance. |
createServer() | Create a net.Server object. |
prompt.get() | This function is used for I/O operations. |
aborted() | Check if ‘aborted’ event is emitted or not. |
bufferSize() | Get the number of characters currently buffered to be written. |
Please Login to comment...