Skip to content
Related Articles

Related Articles

Node.js response.hasHeader() Method

View Discussion
Improve Article
Save Article
  • Last Updated : 07 Dec, 2021

The response.hasHeader() (Added in v7.7.0) property is an inbuilt property of the ‘http’ module which returns true if the header identified by name is currently set in the outgoing headers. The header name matching is case-insensitive. The object returned by the response.getHeaders() method does not prototypically inherit from the JavaScript Object. 

In order to get a response and a proper result, we need to import ‘http’ module.

const http = require('http');



Parameters: This property accepts one parameter as mentioned above and described below:

  • name: It is the name of the header value which is to be checked for existence. 

Return Value <Boolean>: It returns True if the header’s name is currently set otherwise returns False.

The below example illustrates the use of response.hasHeader() property in Node.js.

Example 1: Filename: index.js


// Node.js program to demonstrate the
// response.hasHeaders() Method
// Importing http module
var http = require('http');
// Setting up PORT
const PORT = process.env.PORT || 3000;
// Creating http Server
var httpServer = http.createServer(
    function (req, response) {
    // Setting up Headers
    response.setHeader('Alfa', 'Beta');
    // response.setHeader('', 'Beta'); // Throws Error
    // response.setHeader(); // Throws Error
    response.setHeader('Alfa1', '');
    response.setHeader('Alfa2', 5);
            ['Alfa=Beta', 'Beta=Romeo']);
    // Checking the headers
    const hasHeardere = response.hasHeader('Cookie-Setup');
    const hasHearder0 = response.hasHeader('Alfa');
    const hasHearder1 = response.hasHeader('Alfa1');
    const hasHearder2 = response.hasHeader('Alfa2');
    const hasHearder3 = response.hasHeader('Content-Type');
    // Printing output
    console.log("When Header is set an Array:", hasHeardere);
    console.log("When Header is set Beta:", hasHearder0);
    console.log("When Header is set '':", hasHearder1);
    console.log("When Header is set number 5:", hasHearder2);
    console.log("When Header is not set:", hasHearder3);
    // Getting the set Headers
    const headers = response.getHeaders();
    // Printing those headers
    var Output = "Hello Geeksforgeeks..., " +
        "When Header is set 'Beta': " + hasHeardere +
        ", When Header is set '':" + hasHearder1 +
        ", When Header is set number 5:" + hasHearder2 +
        ", When Header is not set:" + hasHearder3;
    // Prints Hello Geeksforgeeks...
    // on browser in response
// Listening to http Server
httpServer.listen(PORT, () => {
    console.log("Server is running at port 3000...");

Run index.js file using the following command:

node index.js


Output: In Console…

>> server started at 3000…

When Header is set an Array: true

When Header is set ‘Beta’: true

When Header is set ”:true

When Header is set number 5:true

When Header is not set:false

>> [Object: null prototype] {

alfa: ‘Beta’,

alfa1: ”,

alfa2: 5,

‘cookie-setup’: [‘Alfa=Beta’, ‘Beta=Romeo’]}

// Now run http://localhost:3000/ in the browser.

Output: In Browser…

Hello Geeksforgeeks…, When Header is set ‘Beta’: true, When Header is set ”:true, When Header is set number 5:true,  

When Header is not set:false


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!