Open In App

Node.js response.hasHeader() Method

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

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');

Syntax:

response.hasHeader(name);

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

Javascript




// Node.js program to demonstrate the
// response.hasHeaders() 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 (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);
        response.setHeader('Cookie-Setup',
            ['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
        console.log(headers);
 
        const 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
        response.write(Output);
        response.end();
    });
 
// 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: 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

Reference: https://nodejs.org/api/http.html#http_response_hasheader_name



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads