Node.js Http2ServerResponse.writableEnded Method

Last Updated : 4 Aug, 2026

The Http2ServerResponse.writableEnded property in Node.js indicates whether the response has finished writing data and the writable stream has ended. It helps you check if no more data can be written to the response.

  • It is a read-only property of Http2ServerResponse.
  • It is useful for checking the response state before writing more data.

Syntax:

response.writableEnded

Where

  • response: The Http2ServerResponse object.
  • writableEnded: A boolean indicating whether the response has been ended.

Return Value: This method returns true if and only if the response.writableEnded() method has been called or not.

Example 1:

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

    // Checking for response.end() method
    // using response.writableEnded method
    const value = response.writableEnded;

    // Display the display
    if (value)
        console.log("response.end() method has been called")
    else
        console.log("response.end() method has not been called")
};

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

response.end() api has not been called
status : 200
Received: hello
Received: priority weight : 16
client destroyed
server destroyed

Example 2:  

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

    response.end("the end");

    // Checking for response.end() method
    // using response.writableEnded method
    const value = response.writableEnded;

    // Display the display
    if (value)
        console.log("response.end() method has been called")
    else
        console.log("response.end() method has not been called")
};

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

response.end() api has been called
Received: the end
client destroyed
server destroyed

Use Cases of Http2ServerResponse.writableEnded

  • It checks whether the response has already been ended.
  • It prevents writing data after the response is closed.
  • It helps avoid errors caused by multiple calls to response.end().
  • It is useful for conditional response handling in asynchronous code.

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

Comment

Explore