The http2.constants property in Node.js provides predefined constants used by the http2 module. These constants represent HTTP/2 error codes, header names, and other protocol-related values, making code more readable and easier to maintain.
http2.constantscontains predefined values used while working with HTTP/2 in Node.js.- It includes constants for HTTP/2 error codes, header names, and protocol settings.
- Using these constants eliminates the need to hardcode protocol-specific values.
- It helps improve code readability and reduces the chances of errors.
Syntax:
http2.constantsWhere:
http2: The built-in Node.js HTTP/2 module.constants: An object containing predefined HTTP/2 constants.
Return Value: It returns an object with HTTP/2 constants.
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);
const value = http2.constants
.NGHTTP2_FLOW_CONTROL_ERROR
console.log("error code : " + value)
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:
error code : 3
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);
const value = http2.constants.NGHTTP2_NO_ERROR
console.log("error code : " + value)
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:
error code : 0
Received: world
client closed
server closed
Use Cases of http2.constants Property
- It is used to access predefined HTTP/2 constants.
- It is used to handle HTTP/2 error codes.
- It is used to work with standard HTTP/2 header names.
- It helps avoid hardcoded protocol values.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_http2_constants