The Http2ServerResponse.statusCode property in Node.js is used to get or set the HTTP status code that will be sent to the client. It lets you specify the result of the request, such as success, redirection, client error, or server error, before the response headers are sent.
- It gets or sets the HTTP status code for the response.
- The default value of
statusCodeis200(OK). - It should be set before the response headers are sent to the client.
Syntax:
response.statusCodeWhere:
- response: The
Http2ServerResponseobject that represents the HTTP/2 response. - statusCode: The property used to get or set the HTTP status code.
- code: The numeric HTTP status code to send (for example,
200,404, or500).
Return Value: This method returns an integer value representing the particular status of the data header.
Example 1:
// Node.js program to demonstrate the
// Http2ServerResponse.statusCode 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.statusCode method
const value = response.statusCode;
// Display the display
console.log("status code : " + 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 code : 200
Received: hello
Received: priority weight : 16
client destroyed
server destroyed
Example 2:
// Node.js program to demonstrate the
// Http2ServerResponse.statusCode 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.statusCode method
const value = response.statusCode;
// Display the display
response.end("status code : " + 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:
Received: status code : 200
client destroyed
server destroyed
Use Cases of Http2ServerResponse.statusCode Property
- It is used to indicate that a request was processed successfully.
- It is used to return client error responses, such as
404 Not Foundor400 Bad Request. - It is used to send server error responses, such as
500 Internal Server Error. - It is used to set the appropriate status code before sending the response body.
Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http2.html#http2_response_statuscode