Http2ServerResponse.finished is a boolean property in Node.js HTTP/2 that indicates whether the response has finished. It becomes true after response.end() is called. This property is deprecated, and response.writableEnded should be used instead.
- It indicates whether the HTTP/2 response has finished.
- It becomes
trueafterresponse.end()is called. - It is deprecated, and
response.writableEndedshould be used instead. - It helps determine whether the response has completed.
Syntax:
response.finishedWhere,
response:An instance of theHttp2ServerResponseclass.finished: Aboolean property that indicates whether the HTTP/2 response has finished.
Return Value: This method returns a Boolean value true if and only if this response has completed.
Example 1:
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) => {
const value = response.finished
if(value)
console.log("response has completed");
else
console.log("response has not completed")
};
// 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 has not completed
status : 200
Received: hello
Received: priority weight : 16
client destroyed
server destroyed
Example 2:
// Node.js program to demonstrate the
// Http2ServerResponse.finished 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) => {
response.end("hello world")
const value = response.finished
if(value)
console.log("response has completed");
else
console.log("response has not completed")
};
// 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 has completed
Received: hello world
client destroyed
server destroyed
Use Cases of Http2ServerResponse.finished Method
- It can be used to check whether a response has already been completed.
- It helps prevent writing additional data after the response has ended.
- It can be used to conditionally execute logic based on the response completion status.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_response_finished