The http2.aborted property in Node.js indicates whether an HTTP/2 request was terminated before completion by the client. It helps developers detect interrupted requests and handle them appropriately.
- It becomes
truewhen the client closes the connection early. - It helps avoid unnecessary processing for incomplete requests.
- It is useful for cleanup tasks like stopping ongoing operations.
Syntax:
request.abortedWhere:
- request: The HTTP/2 server request object (
Http2ServerRequest) - aborted: A boolean that is
trueif the request was aborted, otherwisefalse
Return Value: This method returns true if 'aborted' event is emitted otherwise false.
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');
// Checking abort event status
// by using stream.aborted method
const value = stream.aborted;
if(value)
console.log("abort event is emitted");
else
console.log("abort event is not emitted");
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server closed");
})
});
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': '/' });
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:
abort event is not emitted
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');
// Checking abort event status
// by using stream.aborted method
const value = stream.aborted;
if(value)
console.log("abort event is emitted");
else
console.log("abort event is not emitted");
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server closed");
})
});
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': '/' });
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm, ""));
});
req.on('end', () => {
client.close(() => {
console.log("client closed");
})
});
Output:
abort event is not emitted
Received: world
client closed
server closed
Use Cases of http2.aborted Method:
- Detect client disconnects: Check if the request was aborted before completing processing.
- Stop ongoing operations: Cancel tasks like file streaming or database queries.
- Avoid unnecessary work: Prevent further computation when the client is no longer waiting.
- Prevent invalid responses: Ensure no response is sent to a closed connection.
- Logging and monitoring: Track aborted requests for debugging and performance analysis.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_http2stream_aborted