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
const http = require( 'http' );
const PORT = process.env.PORT || 3000;
const httpServer = http.createServer( function (request, response) {
response.writeHead(200, {
'Content-Length' : Buffer.byteLength( "GeeksforGeeks" ),
'Content-Type' : 'text/plain'
})
const value = response.statusMessage;
response.end( "hello World" , 'utf8' , () => {
console.log( "displaying the result..." );
httpServer.close(() => {
console.log( "server is closed" )
})
});
console.log( "status message : " + value)
});
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
const http = require( 'http' );
const http2Handlers = (request, response) => {
response.writeHead(200, {
'Content-Type' : 'text/plain'
}).end( "hello World" , 'utf8' , () => {
console.log( "displaying the result..." );
});
const value = response.statusMessage;
httpServer.close(() => {
console.log( "server is closed" )
})
console.log( "status message : " + value)
};
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