The Http2ServerResponse.end() method in Node.js is used to finish an HTTP/2 response by indicating that no more data will be sent. It can optionally send a final chunk of data before closing the stream.
- It ends the response and closes the HTTP/2 stream.
- It can optionally send a final piece of data before finishing.
- It ensures that any remaining buffered data is flushed to the client.
Syntax:
response.end([data[, encoding]][, callback])Where:
- data (optional): It is the response header to be sent.
- encoding (optional): It is the type of encoding.
- callback (optional): It is the callback function for further operation.
Return Value: It returns the response object, allowing method chaining.
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) => {
response.write("Greetings")
// Sending the signal to client
response.end("hello world")
};
// 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:
status : 200
Received: Greetings
Received: hello world
client destroyed
server destroyed
Example 2:
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) => {
// Sending the signal to client
response.end("Geeks For Geeks ")
};
// 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: Geeks For Geeks
client destroyed
server destroyed
Use Cases of Http2ServerResponse.end() Method:
- It is used to complete an HTTP/2 response after sending all required data.
- It is useful when sending a final chunk of data along with ending the response.
- It helps ensure that the response stream is properly closed.
- It is commonly used after writing data using methods like
write().
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_response_end_data_encoding_callback