The Http2ServerResponse.sendDate property specifies whether Node.js automatically adds the Date header to an HTTP/2 response. It is enabled by default but can be disabled if needed.
- It controls automatic sending of the
Dateheader. - It is enabled by default.
- It is useful when managing headers manually.
Syntax:
response.sendDateWhere:
- response: The
Http2ServerResponseobject representing the HTTP/2 response. - sendDate: A boolean property that gets or sets whether the
Dateheader is sent automatically.
Return Value: It returns a boolean value:
trueif theDateheader will be sent automatically.falseif theDateheader will not be sent automatically.
Example 1:
// Node.js program to demonstrate the
// Http2ServerResponse.sendDate 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 if the data header is sent or not
const value = response.sendDate
if (value)
console.log("data header is sent")
else
console.log("data header is not sent")
};
// 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:
data header is sent
status : 200
Received: hello
Received: priority weight : 16
client destroyed
server destroyed
Example 2:
// Node.js program to demonstrate the
// Http2ServerResponse.sendDate 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 if the data header is sent or not
const value = response.sendDate
if (value)
response.end("data header is sent")
else
response.end("data header is not sent")
};
// 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: data header is sent
client destroyed
server destroyed
Use Cases of Http2ServerResponse.sendDate Property
- It lets you disable the automatic
Dateheader when required. - It helps when your application sets the
Dateheader manually. - It is useful for testing responses with custom headers.
- It gives greater control over HTTP response headers.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_response_senddate