Node.js response.removeHeader() Method
Last Updated :
06 Apr, 2023
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 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
JavaScript
// 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
Similar Reads
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 Pa
2 min read
Node.js response.write() Method
The response.write() (Added in v0.1.29) method is an inbuilt Application program Interface of the âhttpâ module which sends a chunk of the response body that is omitted when the request is a HEAD request. If this method is called and response.writeHead() has not been called, it will switch to implic
3 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: respons
2 min read
Node.js dns.resolveNs() Method
The dns.resolveNs() method is an inbuilt application programming interface of the dns module which is used to resolve NS or name server records for the specified hostname using DNS protocol. Syntax: dns.resolveNs( hostname, callback ) Parameters: This method has two parameters as mentioned above and
2 min read
Node.js stringDecoder.write() Method
The stringDecoder.write() method is used to return a decoded string from the given Buffer, TypedArray or DataView. This method also ensures that any incomplete multibyte characters at the end of the buffer are omitted from the returned string. These characters are stored in an internal buffer for th
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
2 min read
Node.js http.validateHeaderName() Method
The http.validateHeaderName() (Added in v14.3.0) property is an inbuilt property of the âhttpâ module which performs the low-level validations on the provided name that are done when res.setHeader(name, value) is called. Passing an illegal value as the name will result in a TypeError being thrown, i
3 min read
Node.js response.writeContinue() Method
The response.writeContinue() (Added in v0.3.0) method is an inbuilt Application Programming Interface of the âhttpâ module which sends an HTTP/1.1 100 Continue message to the client, indicating that the request body should be sent. See the 'checkContinue' event on Server. The response.writeContinue(
2 min read
Node.js Stream readable.unshift() Method
The readable.unshift() method in a Readable Stream is utilized to push a chunk of data back into the internal buffer. However, when a stream is being fully consumed and it needs to be "un-consumed" then this method is helpful. Syntax: readable.unshift( chunk, encoding ) Parameters: This method accep
2 min read
Node.js Stream readable.unpipe() Method
The readable.unpipe() method in a Readable Stream is used to detach a Writable stream which was previously attached while using the stream.pipe() method. Syntax: readable.unpipe( destination ) Parameters: This method accepts single parameter destination which is the destination of writable stream to
1 min read