Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between res.setHeader and res.header in node.js

What is the difference between res.setHeader and res.header. Which one should be used for enabling CORS? In some pages res.header is used and some pages res.setHeader is used for CORS.

like image 312
Subham Avatar asked Nov 28 '16 09:11

Subham


People also ask

What is RES setHeader in node JS?

setHeader(name, value) (Added in v0. 4.0) method is an inbuilt application programming interface of the 'http' module which sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced.

What is the difference between setHeader and writeHead?

The difference between setHeader and writeHead is that setHeader sets one header at a time, and you can use as many setHeader methods as you need before you send the response.

What is RES end in node JS?

res. end() will end the response process. This method actually comes from Node core, specifically the response. end() method of http. ServerResponse .

What is writeHead?

writeHead() (Added in v1.. 0) property is an inbuilt property of the 'http' module which sends a response header to the request. The status code is a 3-digit HTTP status code, like 404. The last argument, headers, are the response headers.


3 Answers

res.setHeader() is a native method of Node.js and res.header() is an alias of res.set() method from Express framework.

Documentation: res.setHeader(), res.set()

This two methods do exactly the same thing, set the headers HTTP response. The only difference is res.setHeader() allows you only to set a singular header and res.header() will allow you to set multiple headers. So use the one fit with your needs.

like image 68
Zagonine Avatar answered Oct 19 '22 18:10

Zagonine


Perhaps an example can clarify more:

// single field is set 
res.setHeader('content-type', 'application/json');

// multiple files can be set
res.set({
     'content-type': 'application/json',
     'content-length': '100',
     'warning': "with content type charset encoding will be added by default"
  });
like image 39
Ghafoor Avatar answered Oct 19 '22 17:10

Ghafoor


Addition to high-voting answers, set is alias header which calls setHeader to set a header. here is the source code:

res.set =
res.header = function header(field, val) {
  if (arguments.length === 2) {
    var value = Array.isArray(val)
      ? val.map(String)
      : String(val);

    // add charset to content-type
    if (field.toLowerCase() === 'content-type') {
      if (Array.isArray(value)) {
        throw new TypeError('Content-Type cannot be set to an Array');
      }
      if (!charsetRegExp.test(value)) {
        var charset = mime.charsets.lookup(value.split(';')[0]);
        if (charset) value += '; charset=' + charset.toLowerCase();
      }
    }

    this.setHeader(field, value);
  } else {
    for (var key in field) {
      this.set(key, field[key]);
    }
  }
  return this;
};

Also see GitHub here

like image 6
Pylon Avatar answered Oct 19 '22 18:10

Pylon