I'm a beginner in Express.js
and I'm confused by these two keywords: res.end()
and res.send()
.
Are they the same or different?
First of all, res.send()
and res.end()
are not the same.
I would like to make a little bit more emphasis on some key differences between res.end()
& res.send()
with respect to response headers and why they are important.
1. res.send() will check the structure of your output and set header information accordingly.
app.get('/',(req,res)=>{
res.send('<b>hello</b>');
});
app.get('/',(req,res)=>{
res.send({msg:'hello'});
});
Where with res.end() you can only respond with text and it will not set "Content-Type"
app.get('/',(req,res)=>{
res.end('<b>hello</b>');
});
2. res.send() will set "ETag" attribute in the response header
app.get('/',(req,res)=>{
res.send('<b>hello</b>');
});
¿Why is this tag important?
The ETag HTTP response header is an identifier for a specific version of a resource. It allows caches to be more efficient, and saves bandwidth, as a web server does not need to send a full response if the content has not changed.
res.end()
will NOT set this header attribute
.end()
as a low-level function and use .send()
in your Express application?
Sep 10, 2021 at 13:52
First of all, res.send()
and res.end()
are different.
res.send()
will send the HTTP response. Its syntax is,
res.send([body])
The body parameter can be a Buffer object, a String, an object, or an Array. For example:
res.send(new Buffer('whoop'));
res.send({ some: 'json' });
res.send('<p>some html</p>');
res.status(404).send('Sorry, we cannot find that!');
res.status(500).send({ error: 'something blew up' });
See this for more info.
res.end()
will end the response process. This method actually comes from Node core, specifically the response.end()
method of http.ServerResponse
. It is used to quickly end the response without any data. For example:
res.end();
res.status(404).end();
Read this for more info.
express
but from the title thought - one sends something, one ends something.... 'twas right.
Jul 24, 2018 at 15:01
res.send()
with nothing. Does this act like res.end()
?
May 19, 2020 at 12:33
res.send()
implements res.write
, res.setHeaders
and res.end
:
res.write
.res.end
to set the end of the request.There are some cases in which you will want to do this manually, for example, if you want to stream a file or a large data set. In these cases, you will want to set the headers yourself and use res.write
to keep the stream flow.
In addition to the excellent answers, I would like to emphasize here, when to use res.end() and when to use res.send() this was why I originally landed here and I didn't found a solution.
The answer is very simple.
res.end() is used to quickly end the response without sending any data.
An example for this would be starting a process on a server:
app.get(/start-service, (req, res) => {
// Some logic here
exec('./application'); // dummy code
res.end();
});
If you would like to send data in your response then you should use res.send() instead:
app.get(/start-service, (req, res) => {
res.send('{"age":22}');
});
Here you can read more:
res.send()
is used to send the response to the client where res.end()
is used to end the response you are sending.
res.send()
automatically call res.end()
So you don't have to call or mention it after res.send()
res
is an HttpResponse object which extends from OutgoingMessage. res.send
calls res.end
which is implemented by OutgoingMessage to send HTTP response and close connection. We see code here
res.end
used by plain node where asres.send
is used by express framworkresponse.send
includes aresponse.end
call.