Skip to content
Related Articles

Related Articles

Node.js http.ServerResponse.statusMessage Property
  • Last Updated : 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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..."); 
});

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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..."); 
});

chevron_right


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

My Personal Notes arrow_drop_up
Recommended Articles
Page :