The http2.getDefaultSettings() Method is a built-in method of the Node.js HTTP/2 module that returns an object containing the default HTTP/2 settings used when creating HTTP/2 sessions. It is useful for viewing the default configuration values before establishing a connection.
- It returns the default HTTP/2 settings as a JavaScript object.
- It helps inspect the settings that Node.js uses by default.
- It can be used as a reference before applying custom HTTP/2 settings.
Syntax:
http2.getDefaultSettings()Where:
http2: The built-in Node.js HTTP/2 module that provides APIs for creating HTTP/2 clients and servers.getDefaultSettings(): A method of thehttp2module that returns an object containing the default HTTP/2 settings used by Node.js.
Return Value: An object where each property represents a default HTTP/2 setting (such as header table size, initial window size, and maximum frame size).
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'),
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options);
// Getting default setting
// by using getDefaultSetting() method
const value = http2.getDefaultSettings()
console.log("header table size : "
+ value.headerTableSize)
server.on('stream', (stream, requestHeaders) => {
stream.respond({
':status': 200,
'content-type': 'text/plain'
});
stream.write('hello ');
stream.end('world');
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server closed");
})
});
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', (responseHeaders) => {
console.log("status : "
+ responseHeaders[":status"]);
});
req.on('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(() => {
console.log("client closed");
})
});
Output:
header table size : 4096
status : 200
Received: hello
Received: world
client closed
server closed
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'),
};
// Creating and initializing server
// by using http2.createServer() method
const server = http2.createServer(options);
// Getting default setting
// by using getDefaultSetting() method
const value = http2.getDefaultSettings()
console.log("initial window size : "
+ value.initialWindowSize)
server.on('stream', (stream, requestHeaders) => {
stream.end('world');
// Stopping the server
// by using the close() method
server.close(() => {
console.log("server closed");
})
});
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('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(() => {
console.log("client closed");
})
});
Output:
initial window size : 65535
Received: world
client closed
server closed
Use Cases of http2.getDefaultSettings() Method
- It is used to view the default HTTP/2 settings used by Node.js.
- It helps compare the default settings with custom configurations.
- It can be used as a baseline before applying custom HTTP/2 settings.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_http2_getdefaultsettings