The http2session.localSettings property in Node.js provides information about the HTTP/2 settings configured for the local endpoint of a session. It contains the settings that control the behavior of the local HTTP/2 connection.
- It contains the HTTP/2 settings configured for the local endpoint.
- It reflects settings such as the initial window size and maximum frame size.
- It provides a read-only view of the session's local settings.
Syntax:
http2session.localSettingsWhere,
- http2session: An instance of the
ClientHttp2SessionorServerHttp2Sessionclass. - localSettings: A property that provides the HTTP/2 settings for the local endpoint.
Return Value: The property returns an object containing the local HTTP/2 settings.
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() api
const server = http2.createServer(options);
server.on('stream', (stream, requestHeaders) => {
stream.respond({
':status': 200,
'content-type': 'text/plain'
});
stream.write('hello ');
const http2session = stream.session;
// Getting local setting by
// using localSetting API
const status = http2session.localSettings;
stream.end("Max header size : "
+ status.maxHeaderListSize);
// Stopping the server by
// using the close() method
server.close(() => {
console.log("server localSettings");
})
});
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 localSettings");
})
});
Output:
status : 200
Received: hello
Received: Max header size : 4294967295
client localSettings
server localSettings
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() api
const server = http2.createServer(options);
server.on('stream', (stream, requestHeaders) => {
// Getting session object
// by using session api
const http2session = stream.session;
// Getting local setting by
// using localSetting API
const status = http2session.localSettings;
stream.end("header table size : "
+ status.headerTableSize);
// 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('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm,""));
});
req.on('end', () => {
client.close(()=>{
console.log("client destroyed");
})
});
Output:Â
Received: header table size: 4096
client destroyed
server destroyed
Use Cases of http2session.localSettings Method:
- It is used to inspect the settings applied to the local HTTP/2 session.
- It is used to monitor HTTP/2 connection configuration.
- It is used to verify settings such as flow-control and frame-size limits.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_http2session_localsettings