Node.js Http2ServerRequest.destroy() Method

Last Updated : 23 Jul, 2026

The Node.js Http2ServerRequest.destroy() method immediately destroys an HTTP/2 request stream and stops any further processing. It also releases the associated resources and optionally accepts an error object that indicates why the request was destroyed.

  • The destroy() method immediately destroys the HTTP/2 request stream.
  • It stops any further processing of the request.
  • It releases the resources associated with the request stream.

Syntax:

request.destroy([error])

Where

  • request: The Http2ServerRequest object on which the destroy() method is called.
  • destroy(): Destroys the request stream and stops further processing.
  • error (optional): An Error object that specifies the reason for destroying the request.

Return Value: Returns the same request object (Http2ServerRequest).

Example 1:

JavaScript
// Node.js program to demonstrate the
// Http2ServerRequest.destroy 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'),
};

// Request and response handler
const http2Handlers = (request, response) => {

  // Destroying the request 
  // by using request.destroy() method
  request.destroy()
  console.log('request is destroyed')
};

// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options,http2Handlers);

server.on('stream', (stream, requestHeaders) => {
  stream.respond({ ':status': 200, 'content-type': 
  'text/plain' });
  stream.write('hello ');

  // Getting all information of this http2stream object
  // by using state method
  const status = stream.state;

  stream.end("priority weight : " + status.weight);

  stream.destroy();

  // 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:

request is destroyed
status : 200
Received: hello
client destroyed
server destroyed

Example 2:

JavaScript
// Node.js program to demonstrate the
// Http2ServerRequest.destroy 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'),
};

// Request and response handler
const http2Handlers = (request, response) => {

  // Destroying the request 
  // by using request.destroy() method
  request.destroy()
  response.end('request is destroyed')  
};

// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options, http2Handlers);

server.on('stream', (stream, requestHeaders) => {

  // Getting all information of this http2stream object
  // by using state method
  const status = stream.state;

  stream.end("The sum weight of all Http2Stream : " + 
  status.sumDependencyWeight);

  // 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:

Received: request is destroyed 
client destroyed
server destroyed

Use Cases of Http2ServerRequest.destroy() Method

  • To stop processing an invalid or malformed request.
  • To terminate a request when a server-side error occurs.
  • To cancel request handling when it is no longer needed.
  • To close the request stream and free associated resources.

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

Comment

Explore