Node.js http.ServerResponse.statusMessage Property
Last Updated :
06 Apr, 2023
Improve
The httpServerResponse.statusMessage is an inbuilt application programming interface of class ServerResponse within http module which is used to control the status message that will be sent to the client when the headers get flushed.
Syntax:
response.statusMessage
Parameters: This method does not accept any arguments as a parameter.
Return Value: This method returns the status message that will be sent to the client.
Example 1: Filename: index.js
Javascript
// Node.js program to demonstrate the // response.statusMessage APi // Importing http module const http = require( 'http' ); // Setting up PORT const PORT = process.env.PORT || 3000; // Creating http Server const httpServer = http.createServer( function (request, response) { response.writeHead(200, { 'Content-Length' : Buffer.byteLength( "GeeksforGeeks" ), 'Content-Type' : 'text/plain' }) // Getting the statusMessage // by using statusMessage API const value = response.statusMessage; // Display result // by using end() api response.end( "hello World" , 'utf8' , () => { console.log( "displaying the result..." ); httpServer.close(() => { console.log( "server is closed" ) }) }); console.log( "status message : " + value) }); // Listening to http Server httpServer.listen(PORT, () => { console.log( "Server is running at port 3000..." ); }); |
Run the index.js file using the below command:
node index.js
Console output:
Server is running at port 3000... status message : OK displaying the result... server is closed
Example 2: Filename: index.js
Javascript
// Node.js program to demonstrate the // response.statusMessage APi // Importing http module const http = require( 'http' ); // Request and response handler const http2Handlers = (request, response) => { response.writeHead(200, { 'Content-Type' : 'text/plain' }).end( "hello World" , 'utf8' , () => { console.log( "displaying the result..." ); }); // Getting the statusMessage // by using statusMessage API const value = response.statusMessage; httpServer.close(() => { console.log( "server is closed" ) }) console.log( "status message : " + value) }; // Creating http Server const httpServer = http.createServer( http2Handlers).listen(3000, () => { console.log( "Server is running at port 3000..." ); }); |
Run the index.js file using the below command:
node index.js
Console output:
Server is running at port 3000... status message : OK displaying the result... server is closed
Browser output:
hello world
Reference:https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_response_statusmessage