The Http2ServerResponse.write() method in Node.js writes a chunk of data to the response body. It allows data to be sent to the client before the response is ended.
- It writes a data chunk to the response.
- It can be called multiple times before
end(). - It is useful for sending data in multiple chunks.
Syntax:
response.write(chunk[, encoding][, callback]);Parameters: This method accepts the following argument as a parameter.
- response: The
Http2ServerResponseobject used to send the HTTP/2 response to the client. - chunk: The data to write to the response body. It can be a
string,Buffer,Uint8Array, orDataView. - encoding (optional): The character encoding to use when
chunkis a string (for example,'utf8'). - callback (optional): A function that is called after the data has been flushed to the stream.
Return Value: The response.write() method returns a boolean:
true: The data was accepted for writing.false: The internal buffer is full, so wait for the'drain'event before writing more data.
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) => {
// Sending the data to the client
// by using response.write() method
response.write("hi")
// 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: hi
Received: hello world
client destroyed
server destroyedExample 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 data to the client
// by using response.write() method
response.write("GeeksForGeeks")
};
// 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: GeeksForGeeks
Received: The sum weight of all Http2Stream : 0
client destroyed
server destroyedUse Cases of Http2ServerResponse.write() Method
- Sending response data to the client in chunks.
- Streaming large files or generated content.
- Sending data before the entire response is ready.
- Building efficient HTTP/2 streaming applications.