Node.js http.ServerResponse.statusMessage Property - GeeksforGeeks

20 Jan, 2021
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 var http = require('http');     // Setting up PORT const PORT = process.env.PORT || 3000;     // Creating http Server var 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 index.js file using 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 var 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 var httpServer = http.createServer(    http2Handlers).listen(3000, () => {     console.log("Server is running at port 3000..."); });
Run index.js file using 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
Node.js-Methods
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Express.js express.Router() Function
JWT Authentication with Node.js
Express.js req.params Property
Mongoose Populate() Method
Difference between npm i and npm ci in Node.js
Roadmap to Become a Web Developer in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Convert a string to an integer in JavaScript