Node.js http2stream.destroyed Method

Last Updated : 8 Sep, 2026

The http2stream.destroyed property in Node.js is used to check the current state of an Http2Stream. It indicates whether the stream has been destroyed and is no longer available for normal operations.

  • It provides information about whether the HTTP/2 stream has been destroyed.
  • It can be checked throughout the lifecycle of an Http2Stream.
  • It helps determine whether the stream is still available for use.

Syntax

http2stream.destroyed

Where,

  • http2stream: An instance of the Http2Stream class that represents an HTTP/2 communication stream.
  • destroyed: A property that indicates whether the stream has been destroyed.

Return Value:

  • The property returns true when the stream has been destroyed.
  • The property returns false when the stream has not been destroyed.

Example 1:

javascript
// Node.js program to demonstrate the
// Http2Stream.destroyed 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 ');

   // Checking if stream is destroyed or not
   // by using destroyed() method
   const status = stream.destroyed;

   if(status)
     stream.end("stream is destroyed");
   else
     stream.end("stream is not destroyed");

   // 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('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: stream is not destroyed
client destroyed
server destroyed

Example 2:

javascript
// Node.js program to demonstrate the
// Http2Stream.destroyed 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) => {
 
   // Destroying  the stream 
   // by using destroy() method
   stream.destroy();

   // Checking if stream is destroyed or not
   // by using destroyed() method
   const status = stream.destroyed;

   if(status)
     console.log("stream is destroyed");
   else
     console.log("stream is not destroyed");
  
   // 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");
  })
});

Output:

stream is destroyed
client destroyed
server destroyed

Use Cases of http2stream.destroyed Method:

  • It is used to check whether an HTTP/2 stream has been destroyed.
  • It is used to prevent operations on a destroyed stream.
  • It is used to monitor the lifecycle of an HTTP/2 stream.

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_http2stream_destroyed

Comment

Explore