admin管理员组文章数量:1277267
What is the difference between the following
response.status(200).send('Hello World!');
and this
response.writeHead(200, {'content-type':'application/json'});
response.end('Hello World!');
Which of these should be used when?
What is the difference between the following
response.status(200).send('Hello World!');
and this
response.writeHead(200, {'content-type':'application/json'});
response.end('Hello World!');
Which of these should be used when?
Share Improve this question asked Oct 28, 2019 at 18:10 user6184932user6184932 1-
1
Why are you setting content-type to
application/json
and then sending plain text? – jfriend00 Commented Oct 28, 2019 at 18:37
3 Answers
Reset to default 7response.status(...)
assigns a status code that will be used in the future. The headers are not sent until the response body is sent.
response.writeHead(...)
actually queues the response headers to be sent out immediately. That means you can no-longer assign any headers or status codes for that response afterward since all of those have already been sent out.
Typically you'd use the former pattern, but .writeHead
is useful if you want to get time-to-first-byte (TTFB) happening sooner.
From the statusCode
point of view, all of these are identical:
- Calling
res.status(statusCode)
- Setting the
res.statusCode
property directly - Passing a statusCode to
res.writeHead(statusCode, ...)
- Calling
res.sendStatus(statusCode)
For the statusCode itself, they all do the same thing which is to set the .statusCode
property. That property value is then used whenever the response sends headers (which might be immediately or might be later depending upon which option you chose). You can see this by looking at the Express and http source code (references below).
Calling res.status()
and setting res.statusCode
just set the property for later use.
Calling res.writeHead()
sets the res.statusCode
property and will then cause the headers to be flushed immediately, rather than their normal behavior of being queued to be sent later. But, from the statusCode point of view, there is no difference.
Calling res.sendStatus()
set the res.statusCode
property and will then cause the response to be sent immediately with just headers, no body. This is often used for responses that need no body (redirects, error codes, etc...).
For questions like this, you can just go look at the source code. For .status()
, we see this:
res.status = function status(code) {
this.statusCode = code;
return this;
};
Then, in the nodejs code for an http.ServerResponse
, you can see that calling .writeHead()
yourself, just ends up setting the same this.statusCode
property before it calls .end()
to flush out the headers and the rest of the response:
And, if you then look at the doc for http.ServerResponse
, you see this:
response.statusCode
Added in: v0.4.0
Default: 200
When using implicit headers (not calling response.writeHead() explicitly), this property controls the status code that will be sent to the client when the headers get flushed.
So, .status()
is just a method for setting the statusCode
property which is what the response value will be sent to when the headers are eventually sent (in the future) and (from the statusCode point of view) is exactly the same as calling writeHead()
and passing it the status.
Express is trying to play at a little bit higher level of abstraction so they created a method for setting the statusCode
separately which can be called at any time before the response is set.
In addition to Jacob's answer, response.status
is not part of the NodeJS standard library (See the docs), but is actually third party
本文标签: javascriptDifference between responsestatus and responsewriteHeadStack Overflow
版权声明:本文标题:javascript - Difference between response.status and response.writeHead? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741290676a2370529.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论