javascript - Difference between response.setHeader and response.writeHead? -
In my application, I have my node servers that send a JSON response. I found two ways to do this, but I'm not sure what the difference is.
One way is
var json = JSON.stringify (result.rows); Response.writeHead (200, {'content-type': 'application / json', 'content-length': buffer.bitelemth (JSON)}); Response.end (JSON); and my second method
var json = JSON.stringify (result.rows); Response.setHeader ('content-type', 'app / jason'); Response.end (JSON); Both work in the way and I'm just wondering what the difference is between the two and when I should use one on the other.
response.setHeader () only singular > Allows setting the header.
response.writeHead () will allow you to set a lot about the status code, the content, and the response head with the Multiple header.
Consider the API:
sets a single header value for the underlying header if the headers are already in the header If present, then its value will be changed. Use strings here if you need 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);
Sends a feedback header to the request. The status code is a 3-digit HTTP status code, such as 404. Final logic, headers, feedback headings are alternatively another argument can be made in the form of a second human argument.
var body = "Hello World"; Response.writeHead (200, {"content-length": body.length, "content-type": "text / plain", "set-cookie": "type = ninja"});
Comments
Post a Comment