The Http2ServerRequest close event is emitted when the underlying HTTP/2 stream associated with the request is closed. It allows the server to detect the end of a request, making it useful for performing cleanup tasks or finalizing operations after the request is no longer active.
- It is emitted when the underlying
Http2Streamis closed. - It belongs to the
Http2ServerRequestclass in Node.js HTTP/2. - It is triggered automatically by the HTTP/2 request lifecycle.
Syntax:
request.on('close', listener);Where:
request: An instance ofHttp2ServerRequest.'close': The event emitted when the request's underlying HTTP/2 stream is closed.listener: The callback function executed when thecloseevent occurs.
Return Value: Returns the Http2ServerRequest instance, enabling method chaining.
Example 1:
// Node.js program to demonstrate the
// Http2ServerRequest 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) => {
// Creating and initializing the http2 server request
const request = new http2.Http2ServerRequest(stream)
// Emitting close event
request.on('close' , () => {
console.log("request is closed")
})
// Setting additional header
// by using additionalHeaders() method
stream.additionalHeaders({ ':status': 100, 'content-type':
'text/plain' });
stream.end("the end");
// Closing the stream event
stream.close();
// 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('http://localhost:8000');
const req = client.request({ ':method': 'GET',
':path': '/' });
// Emitting header event
req.on('headers', (headers, flags) => {
console.log(headers);
});
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(() => {
console.log("client destroyed");
})
});
Output:
request is closed
[Object: null prototype] {
':status': 100,
'content-type': 'text/plain'
}
Received: the end
client destroyed
server destroyed
Example 2:
// Node.js program to demonstrate the
// Http2ServerRequest 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) => {
// Creating and initializing the http2 server request
const request = new http2.Http2ServerRequest(stream)
// Emitting close event
request.on('close' , ()=>{
console.log("request is closed")
})
// Setting additional header
// by using additionalHeaders() method
stream.additionalHeaders({
':status': 100,
'content-type': 'text/plain'
});
// Getting rst stream code
// by using sentInfoHeaders method
const status = stream.sentInfoHeaders;
console.log(status)
// Closing the stream event
stream.close();
// 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(
'http://localhost:8000');
const req = client.request({
':method': 'GET', ':path': '/' });
// Emitting header event
req.on('headers', (headers, flags) => {
console.log(headers);
client.close();
});
Output:
[
[Object: null prototype] {
':status': 100,
'content-type': 'text/plain'
}
]
request is closed
[Object: null prototype] {
':status': 100,
'content-type': 'text/plain'
}
server destroyed
Use Cases of Http2ServerRequest close Event:
- Cleaning up resources after a request ends.
- Detecting when an HTTP/2 request has been closed.
- Finalizing request-related operations.
- Logging request completion or closure events.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_event_close_2