error display: can’t set headers after they are sent after they are sent because
server will output the response header and then the body when processing HTTP request, and once output the response header (for example: Res.writehead () or res.write() or res.end()) and an error is reported when we again try to set the response header via res.setheader () or res.writehead () (some methods such as Res.Redirect () call res.writehead ().
when I change res.send() to res.end(), the code runs successfully
What’s the difference between res.send() and Res.end ()?
Res.end
if there is no data returned to the client from the server then res.end
but if there is data returned to the client from the server then res.send must be used instead of res.end
Here is an excerpt of how res.send() is used:
res.send([body|status], [body])
You can either send the content directly, or you can send the first parameter status and the second parameter content.
If the content is sent directly, the status will be completed automatically.
example:
res.send(newBuffer(‘ whoop ‘));
res.send({some: ‘json’});
res. Send (” some HTML “);
res.send(404, Sorry, we cannot find that! ‘);
res.send(500, {error: ‘something blew up’});
res.send(200);
[1] The first way is to send binary Content. When its parameter is Buffer, content-type will be set to “Application /octet-stream”. This means that its file suffix (file Type) is of certain types.
and http://www.w3school.com.cn/media/media_mimeref.asp wiki says
application/octet – stream (arbitrary binary data)
that is to say, this is an arbitrary binary data, how to explain the specific depends on actual situation (such as suffix), such as he may be a img, could also be a video.
[2] If a string is sent, it will be interpreted as an HTML file.
that is, content-type is set to “text/ HTML” by default:
for example, send a post, then I res.send(” aaa “), then the page will jump to a page with only text aaa;
[3] If the parameter is Array, or Ojbect (object), then a JSON is returned.
Read More:
- UnhandledPromiseRejectionWarning Error: Can‘t set headers after they are sent (How to Fix)
- The problem that headers [‘content-type ‘] does not work is set in the Axios get method request interface
- Failed to set session cookie. Maybe you are using HTTP instead of HTTPS to access phpMyAdmin.
- What are the web front end technologies? What are the differences between cookie and session
- A PHP Error was encountered Severity: Warning Message: mysqli::real_connect(): Headers and client
- IIS “Bad Request – Request Too Long. HTTP Error 400. The size of the request headers is too long.”
- File contains no section headers. File: File: / / etc/ yum.repos .d/
- Bad Request – Request Too Long. HTTP Error 400. The size of the request headers is too long
- Error in installing torch vision or pilot on Linux or Jetson nano: the headers or library files could not be found for JPEG
- Istio Error 503 upstream connect error or disconnect/reset before headers. reset reason: connect
- MySQL modify character set
- Cross origin requests are only supported for
- El expressions are not parsed and output as is
- [Space] There are many right and wrong before and after, it can cause major failures, please see
- This application has no explicit mapping for /error, so you are seeing this as a fallback (How to Fix)
- RabbitMQ Startup Script (How to Set)
- Lambda set to map duplicate key error solution
- How to use dangerously set inner HTML in react
- Error reporting when Lombok @ data and @ builder are used together
- quill Cannot import ImageResize. Are you sure it was registered?