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
// Node.js program to demonstrate the // response.removeHeader() 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( '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 Server httpServer.listen(PORT, () => { console.log( "Server is running at port 3000..." );
}); |
Now run http://localhost:3000/ in the browser.
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
// Node.js program to demonstrate the // response.removeHeader() 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( '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);
const Output = "Hello GeeksforGeeks...,"
+ " Available headers are:"
+ JSON.stringify(headers);
// Prints Output 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 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