Open In App

Node.js http.ServerResponse.headersSent Property

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The httpServerResponse.headersSent is an inbuilt application programming interface of class ServerResponse within the HTTP module which is used to check if the header has been sent or not.

Syntax:

const response.headersSent

Parameters: This property does not accept any arguments as a parameter.

Return Value: This property returns true if and only if headers were sent otherwise false.

Example 1: Filename-index.js

Javascript




// Node.js program to demonstrate the
// response.headersSent() method
 
// 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) {
 
        // Checking if response header is sent or not
        // by using headersSent method
        const value = response.headersSent;
 
        // Display result by using end() method
        response.end("Headers have been sent : "
            + value, 'utf8', () => {
                console.log("displaying the result...");
 
                httpServer.close(() => {
                    console.log("server is closed")
                })
            });
    });
 
// Listening to http Server
httpServer.listen(PORT, () => {
    console.log("Server is running at port 3000...");
});


Run the index.js file using the following command:

node index.js

Output:

Server is running at port 3000...
displaying the result...
server is closed

Now open your browser and go to http://localhost:3000/, you will see the following output:

Headers have been sent : false

Example 2: Filename-index.js

Javascript




// Node.js program to demonstrate the
// response.headersSent() method
 
// Importing http module
const http = require('http');
 
// Request and response handler
const httpHandlers = (request, response) => {
    // Checking if response header is sent or not
    // by using headersSent method
    const value = response.headersSent;
 
    // Display result by using end() method
    response.end("Headers have been sent : "
        + value, 'utf8', () => {
            console.log("displaying the result...");
 
            httpServer.close(() => {
                console.log("server is closed")
            })
        });
};
 
// Creating http Server
const httpServer = http.createServer(
    httpHandlers).listen(3000, () => {
        console.log("Server is running at port 3000...");
    });


Run the index.js file using the following command:

node index.js

Output:

Server is running at port 3000...
displaying the result...
server is closed

Now open your browser and go to http://localhost:3000/, you will see the following output:

Headers have been sent : false

Reference: https://nodejs.org/dist/latest-v12.x/docs/api/http.html#http_response_headerssent



Previous Article
Next Article

Similar Reads

Node.js http.ServerResponse.writableFinished Property
The httpServerResponse.writableFinished is an inbuilt application programming interface of class ServerResponse within http module which is used to check if all the data has been flushed or not. Syntax: response.writableFinished Parameters: This property does not accept any arguments as a parameter. Return Value: This property returns true if and o
2 min read
Node.js http.ServerResponse.statusCode Property
The httpServerResponse.statusCode is an inbuilt application programming interface of class ServerResponse within the HTTP module which is used to this property controls the status code that will be sent to the client when the headers get flushed. Syntax: const response.statusCodeParameters: This property does not accept any arguments as a parameter
2 min read
Node.js http.ServerResponse.writableEnded Property
The httpServerResponse.writableEnded is an inbuilt application programming interface of class Server Response within http module which is used to check if response.end() has been called or not. Syntax: response.writableEnded Parameters: This method does not accept any parameter. Return Value: This method returns true if and only if response.end() h
2 min read
Node.js http.ServerResponse.statusMessage Property
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: T
2 min read
Node.js http.ServerResponse.connection Method
The httpServerResponse.connection is an inbuilt application programming interface of class Server Response within http module which is used to get the response socket of this HTTP connection. Syntax: response.connection Parameters: This method does not accept any argument as a parameter. Return Value: This method returns the response socket of this
2 min read
Node.js http.ServerResponse.end() Method
The httpServerResponse.end() is an inbuilt application programming interface of class Server Response within http module which is used to send the signal to the server that all the header has been sent. Syntax: response.end(data, Encodingtype, Callbackfunction) Parameters: This method takes three Parameters Data: Chunk of data that has to be sentEn
2 min read
Node.js http.ServerResponse.getHeader() Method
The httpServerResponse.getHeader() is an inbuilt application programming interface of the class Server Response within http module which is used to get the response header of the particular name. Syntax: response.getHeader(name) Parameters: This method accepts the name of the header as the parameter. Return Value: This method returns the value of t
2 min read
Node.js http.ServerResponse.sendDate Method
The httpServerResponse.sendDate is an inbuilt application programming interface of class Server Response within http module which is used to check if the date header has been sent or not. HTTP header is also used to pass additional information such as Date etc. Refer to this article. Syntax: response.sendDate Parameters: This method does not accept
2 min read
Node.js http.ServerResponse.writeProcessing() Method
The httpServerResponse.writeProcessing() is an inbuilt application programming interface of class ServerResponse within the HTTP module which is used to send an HTTP/1.1 102 Processing message to the client. Syntax: response.writeProcessing()Parameters: This method does not accept any arguments as a parameter. Return Value: This method has nothing
2 min read
Node.js http.ServerResponse.socket Api
The httpServerResponse.socket is an inbuilt application programming interface of class ServerResponse within http module which is used to get the reference of the underlying socket object. Syntax: response.socket Parameters: This method does not accept any arguments as a parameter. Return Value: This method returns the reference of the underlying s
2 min read