50

In my application, I have my Nodejs server send a JSON response. I found two ways to do this but I'm not sure what the differences are.

One way is

var json = JSON.stringify(result.rows);
response.writeHead(200, {'content-type':'application/json', 'content-length':Buffer.byteLength(json)}); 
response.end(json);

And my other way is

var json = JSON.stringify(result.rows);
response.setHeader('Content-Type', 'application/json');
response.end(json);

Both ways work and I'm just wondering what the difference is between the two and when I should use one over the other.

4
  • Docs? nodejs.org/api/… Jan 22, 2015 at 16:51
  • 2
    Sure, I have read the docs. But my knowledge on response headers are limited and just wanted to mainly know when I should use one way over the other.
    – cYn
    Jan 22, 2015 at 16:54
  • 6
    When you want to set a single header without setting statusCode use setHeader, when you want to set statusCode and a bunch of headers (optionally) use writeHead. (c) Captain Obvious Jan 22, 2015 at 16:58
  • 11
    People on S.O. are so cruel with their downvotes. I see absolutely nothing wrong with this question.
    – Anthony
    Aug 8, 2015 at 23:29

1 Answer 1

78

response.setHeader() allows you only to set a singular header.

response.writeHead() will allow you to set pretty much everything about the response head including status code, content, and multiple headers.

Consider the NodeJS docs:

response.setHeader(name, value)

Sets a single header value for implicit headers. If this header already exists in the to-be-sent headers, its value will be replaced. Use an array of strings here to send multiple headers with the same name.

var body = "hello world";
response.setHeader("Content-Length", body.length);
response.setHeader("Content-Type", "text/plain");
response.setHeader("Set-Cookie", "type=ninja");
response.status(200);

response.writeHead(statusCode[, statusMessage][, headers]))

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. Optionally one can give a human-readable statusMessage as the second argument.

var body = "hello world";
response.writeHead(200, {
    "Content-Length": body.length,
    "Content-Type": "text/plain",
    "Set-Cookie": "type=ninja"
});
5
  • 5
    This answer is guiding me into the direction of understanding it a bit more. Thank you for this. As you can see, both methods looks almost identical in code, thus causing my confusion. So I just wasn't understanding why I would use setHeader over writeHead if at the basic level, they're both doing the same thing.
    – cYn
    Jan 22, 2015 at 17:20
  • res.statusCode = 200; not response.status(200); Tested in Node.js v5 Nov 19, 2015 at 9:05
  • 1
    @Bhaveshkumar I'm seeing res.status() as valid and in the documentation.
    – zero298
    Oct 19, 2016 at 14:10
  • 2
    response.writeHead(200, { "Content-Type": "text/html"}); once header is set using writeHead it cannot be changed again this will also override headers set by setHeader nodejs.org/dist/latest-v13.x/docs/api/… Feb 10, 2020 at 19:14
  • 6
    The other thing you don't mention here is that once you call response.writeHead(), the headers are sent and you can no longer set any more headers. Whereas response.setHeader() just configures what headers will be sent when they are sent sometime in the future and you can continue to set or modify headers until they are actually sent.
    – jfriend00
    Jun 28, 2020 at 21:13

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.