Node.js Http2ServerRequest.aborted is a boolean property that indicates whether the request has been aborted before it completed. In HTTP/2 applications, it helps determine whether the client disconnected or the stream was closed prematurely, making it useful for handling incomplete requests in a reliable way.
- The property is read-only, so it cannot be manually assigned a new value.
- It is available on
Http2ServerRequestobjects in Node.js HTTP/2 server code. - It helps server-side code detect premature request termination and react accordingly.
Syntax:
request.abortedWhere:
request: The incomingHttp2ServerRequestobject.aborted: Returnstrueif the request was aborted; otherwise,false.
Return Value: The Http2ServerRequest.aborted property returns a boolean:
true: If the request has been aborted.false: If the request has not been aborted.
Example 1:
// Node.js program to demonstrate the
// request.aborted property
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 ');
const http2session = stream.session;
// Getting state associated with this
// session by using state API
const status = http2session.state;
stream.end("local window size : "
+ status.localWindowSize);
// Stopping the server by
// using the close() method
server.close(()=>{
console.log("server localSettings");
})
});
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 localSettings");
})
const status = req.aborted;
if(status)
console.log("request is aborted")
else
console.log("request is not aborted");
});
Output:
status : 200re
Received: hello
Received: local window size : 65535
request is not aborted
client localSettings
server localSettings
Example 2:
// Node.js program to demonstrate the
// request.aborted property
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) => {
// Getting session object
// by using session api
const http2session = stream.session;
// Getting state associated with this
// session by using state API
const status = http2session.state;
stream.end("numeric id of recent data : "
+ status.lastProcStreamID);
// 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': '/' });
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(()=>{
console.log("client destroyed");
})
const status = req.aborted;
if(status)
console.log("request is aborted")
else
console.log("request is not aborted");
});
Output:
Received: numeric id of recent data : 1
request is not aborted
client destroyed
server destroyed
Use Cases of Http2ServerRequest.aborted
- Detecting whether a client has disconnected before the request completes.
- Preventing further processing of an aborted request.
- Cleaning up resources associated with incomplete requests.
- Avoiding unnecessary operations on terminated HTTP/2 streams.