Skip to content
Related Articles

Related Articles

Node.js response.getHeaders() Method

View Discussion
Improve Article
Save Article
  • Last Updated : 15 Sep, 2020

The response.getHeaders() (Added in v7.7.0) method is an inbuilt method of the ‘http’ module which returns a shallow copy of the current outgoing headers. Since a shallow copy is used, array values may be mutated without additional calls to various header-related http module methods. The keys of the returned object are the header names and the values are the respective header values. All header names are lowercase.

The object returned by the response.getHeaders() method does not prototypically inherit from the JavaScript Object. This means that typical Object methods such as obj.toString(), obj.hasOwnProperty(), and others are not defined and will not work.

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

const http = require('http');



Parameters: This property does not accept any parameters.

Return Value <Object>: It returns a shallow copy of the current outgoing headers. 

The below examples illustrate the use of response.getHeaders() property in Node.js.

Example: Filename: index.js

// Node.js program to demonstrate the 
// response.getHeaders() 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');
        ['Alfa=Beta', 'Beta=Romeo']);
  // Getting the set Headers
  const headers = response.getHeaders();
  // Printing those headers
  // Prints Hello GeeksforGeeks... 
  // on browser in response
  response.write('Hello GeeksforGeeks...');
// Listening to http Server
httpServer.listen(PORT, () => {
    console.log("server started at 3000...")


In Console:
>> server started at 3000…
>> [Object: null prototype] {
alfa: ‘Beta’,
‘cookie-setup’: [‘Alfa=Beta’, ‘Beta=Romeo’]}
>> [Object: null prototype] {
alfa: ‘Beta’,
‘cookie-setup’: [‘Alfa=Beta’, ‘Beta=Romeo’]}

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

Output: (In Browser)

Hello GeeksforGeeks...


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!