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.
setHeader
, when you want to set statusCode and a bunch of headers (optionally) usewriteHead
. (c) Captain Obvious