Node.js Http2ServerResponse.stream Method

Last Updated : 4 Aug, 2026

The Http2ServerResponse.stream property in Node.js returns the underlying HTTP/2 stream associated with a server response. It is useful for accessing low-level stream functionality when working directly with HTTP/2 features.

  • The stream property returns the associated ServerHttp2Stream object.
  • It provides access to HTTP/2 stream-specific methods and events.
  • The property is read-only and cannot be modified.
  • It is mainly used for advanced HTTP/2 stream operations.

Syntax:

response.stream

Where:

  • response: The Http2ServerResponse object for the HTTP/2 response.
  • stream: A property that returns the underlying ServerHttp2Stream instance.

Return Value: Returns the underlying ServerHttp2Stream object.

Example 1:

JavaScript
// Node.js program to demonstrate the
// Http2ServerResponse.stream 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) => {

    // Getting http2 stream reference by 
    // using response.stream method
    const value = response.stream;

    // Display the display
    console.log("id of stream :- " + value.id)
};

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

server.on('stream', (stream, requestheader) => {
    stream.write('hello ');

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

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

    // 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', (responsesocket) => {
    console.log("status : " + responsesocket[":status"]);
});

req.on('data', (data) => {
    console.log('Received: %s ',
    data.toString().replace(/(\n)/gm, ""));
});

req.on('end', () => {
    client.close(() => {
        console.log("client destroyed");
    })
});

Output:

id of stream :- 1
status : 200
Received: hello
Received: priority weight : 16
client destroyed
server destroyed

Example 2:

JavaScript
// Node.js program to demonstrate the
// Http2ServerResponse.stream method
const http2 = require('http2');
const fs = require('fs');
const { RSA_NO_PADDING } = require('constants');

// 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) => {

    // Getting http2 stream reference by 
    // using response.stream method
    const value = response.stream;

    // Display the display
    response.end("id of stream :- " + value.id)
};

// 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': '/www.geeksforgeeks.org' });

req.on('data', (data) => {
    console.log('Received: %s ',
    data.toString().replace(/(\n)/gm, ""));
});

req.on('end', () => {
    client.close(() => {
        console.log("client destroyed");
    })
});

Output:

Received: id of stream :- 1
client destroyed
server destroyed

Use Cases of Http2ServerResponse.stream

  • Access the underlying HTTP/2 stream for advanced control.
  • Handle stream-level events during the response lifecycle.
  • Retrieve stream information for debugging or monitoring.
  • Perform low-level HTTP/2 operations when needed.

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

Comment

Explore