Node.js response.removeHeader() Method - GeeksforGeeks

07 Dec, 2021
The response.removeHeader() (Added in v0.9.3) property is an inbuilt property of the ‘http’ module which removes a header identified by a name that’s queued for implicit sending. 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.removeHeader(name);
Parameters: This property accepts a single parameter as mentioned above and described below:
name <String>: It accepts the name of the header and it is case-insensitive.
Return Value: It does not return any value, instead removes a header that’s queued for implicit sending.
The below example illustrates the use of response.removeHeader() property in Node.js.
Example 1: Filename: index.js
Javascript
// Node.js program to demonstrate the// response.removeHeader() Method // Importing http modulevar http = require('http'); // Setting up PORTconst PORT = process.env.PORT || 3000; // Creating http Servervar httpServer = http.createServer(           function(req, response) {   // Setting up Headers  response.setHeader('Alfa', 'Beta');  response.setHeader('Cookie-Setup',         ['Alfa=Beta', 'Beta=Romeo']);   console.log("Before removing headers...")     // Checking and  printing the headers  console.log("When Header is set an Array:",  response.hasHeader('Cookie-Setup'));   // Removing Headers  response.removeHeader('Cookie-Setup');   console.log("After removing headers...")     // Checking and  printing the headers  console.log("When Header is set an Array:",  response.hasHeader('Cookie-Setup'));   // Printing those headers  console.log(response.getHeaders());   response.end();}); // Listening to http ServerhttpServer.listen(PORT, () => {    console.log("Server is running at port 3000...");});
Now run http://localhost:3000/ in the browser.
Output:
Output: (In Console)
>> Server is running at port 3000...
>> Before removing headers...
   When Header is set an Array: true
>> After removing headers...
   When Header is set an Array: false
>> [Object: null prototype] { alfa: ‘Beta’}
Example 2: Filename: index.js
Javascript
// Node.js program to demonstrate the// response.removeHeader() Method // Importing http modulevar http = require('http'); // Setting up PORTconst PORT = process.env.PORT || 3000; // Creating http Servervar httpServer = http.createServer(           function(req, response) {   // Setting up Headers  response.setHeader('Alfa', 'Beta');  response.setHeader('Alfa1', '');  response.setHeader('Alfa2', 5);  response.setHeader('Cookie-Setup',  ['Alfa=Beta', 'Beta=Romeo']);   console.log("Before removing headers...")     // Checking and  printing the headers  console.log("When Header is set an Array:",  response.hasHeader('Cookie-Setup'));  console.log("When Header is set an 'Beta':",  response.hasHeader('Alfa'));  console.log("When Header is set '':",  response.hasHeader('Alfa1'));  console.log("When Header is set number 5:",  response.hasHeader('alfa2'));  console.log("When Header is not set:",  response.hasHeader('Content-Type'));   // Removing Headers  response.removeHeader('Cookie-Setup');  response.removeHeader('ALFA2');   console.log("After removing headers...")     // Checking and  printing the headers  console.log("When Header is set an Array:",  response.hasHeader('Cookie-Setup'));  console.log("When Header is set an 'Beta':",  response.hasHeader('Alfa'));  console.log("When Header is set '':",  response.hasHeader('Alfa1'));  console.log("When Header is set number 5:",  response.hasHeader('alfa2'));  console.log("When Header is not set:",  response.hasHeader('Content-Type'));   // Getting the set Headers  const headers = response.getHeaders();     // Printing those headers  console.log(headers);   var Output = "Hello GeeksforGeeks...,"    + " Available headers are:"    + JSON.stringify(headers);   // Prints Output on browser in response  response.write(Output);  response.end();}); // Listening to http ServerhttpServer.listen(PORT, () => {    console.log("Server is running at port 3000...");});
Run index.js file using the following command:
node index.js
Output:
Output: (In Console)
>> Server is running at port 3000...
>> Before removing headers...
    When Header is set an Array: true
    When Header is set an ‘Beta’: true
    When Header is set ”: true
    When Header is set number 5: true
    When Header is not set: false
>> After removing headers...
    When Header is set an Array: false
    When Header is set an ‘Beta’: true
    When Header is set ”: true
    When Header is set number 5: false
    When Header is not set: false
>> [Object: null prototype] { alfa: ‘Beta’, alfa1: ” }
Now run http://localhost:3000/ in the browser.
Output: (In Browser)
Hello GeeksforGeeks..., Available headers are:{“alfa”:”Beta”, “alfa1′′:””}
Reference: https://nodejs.org/api/http.html#http_response_removeheader_name
simmytarika5
Node.js-Methods
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to connect Node.js with React.js ?
Difference between dependencies, devDependencies and peerDependencies
Node.js Export Module
Mongoose Populate() Method
Mongoose find() Function
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript