The Http2ServerResponse.statusMessage property in Node.js sets or returns the HTTP status message for a server response. It is used when you need to send a custom status text along with the response status code.
- It stores the status message sent with the response.
- It is mainly used to provide a custom text description for the status code.
- The property is available on
Http2ServerResponseobjects. - It is useful when working with HTTP/2 responses that need clear status information.
Syntax:
response.statusMessageWhere
- response â The
Http2ServerResponseobject. - statusMessage â The HTTP status message property (ignored in HTTP/2).
Return Value: Returns the current HTTP status message as a string. It has no effect in HTTP/2.
Example 1:
// Node.js program to demonstrate the
// Http2ServerResponse.statusMessage 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) => {
// Getting status code
// using response.statusMessage method
const value = response.statusMessage;
// Display the display
console.log("status message : " + value)
};
// 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('data', (data) => {
console.log('Received: %s ',
data.toString().replace(/(\n)/gm, ""));
});
req.on('end', () => {
client.close(() => {
console.log("client destroyed");
})
});
Output:
status message :
(node:656) UnsupportedWarning: Status message is
not supported by HTTP/2 (RFC7540 8.1.2.4)
Received: hello
Received: priority weight : 16
client destroyed
server destroyed
Example 2:
// Node.js program to demonstrate the
// Http2ServerResponse.statusMessage method
const http2 = require('http2');
const fs = require('fs');
const { RSA_NO_PADDING } = require('constants');
// 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) => {
// Getting status code
// using response.statusMessage method
const value = response.statusMessage;
// Display the display
response.end("status message : " + value)
};
// 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:
(node:8360) UnsupportedWarning: Status message is not supported
by HTTP/2 (RFC7540 8.1.2.4)
Received: status message :
client destroyed
server destroyed
Use Cases of Http2ServerResponse.statusMessage Property
- Reading the current HTTP status message.
- Maintaining compatibility with code written for HTTP/1.1.
- Inspecting or debugging response-related code.
- Supporting libraries that reference the
statusMessageproperty.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_response_statusmessage