270

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?

mvg
  • 1,574
  • 4
  • 37
  • 63
sudheeshcm
  • 3,310
  • 4
  • 14
  • 21

6 Answers6

279

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>');
    });

enter image description here


     app.get('/',(req,res)=>{
         res.send({msg:'hello'});
     });

enter image description here

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>');
      }); 

enter image description here

2. res.send() will set "ETag" attribute in the response header

      app.get('/',(req,res)=>{
            res.send('<b>hello</b>');
      });

enter image description here

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

Welcor
  • 2,431
  • 21
  • 32
Jorge González
  • 2,898
  • 1
  • 9
  • 12
  • 24
    This shall be accepted answer in my opinion... more amphasis is made on actual differences that can ruin your work day instead of discussion on ending response with/without content... – Tomas Jan 29 '20 at 09:09
  • 4
    So basically consider `.end()` as a low-level function and use `.send()` in your Express application? – Eric Burel Sep 10 '21 at 13:52
  • Kept it Simple & Silly :) +1 – VishalParkash Dec 31 '21 at 06:25
  • @EricBurel Yes. `.send` calls `.end` internally. – Константин Ван Nov 26 '22 at 17:09
  • this part of the answer is wrong: `Where with res.end() you can only respond with text`. res.send() internally uses `res.setHeader("content-type", myContentType)` to set the content-type and then sends the data using `res.end(data...)`. `res.end()` can send all kind of data, not just text – Welcor Jun 18 '23 at 09:43
203

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.

Kostas Minaidis
  • 4,681
  • 3
  • 17
  • 25
itzmebibin
  • 9,199
  • 8
  • 48
  • 62
  • 99
    But res.end can actually act like res.send, in that you can pass a string argument to add as the response body. On top of that, res.send also ends the response. So how are they functionally different? – saltthehash Jun 11 '16 at 22:43
  • 45
    @psytech140 Jmar77 had a nice response [here](http://stackoverflow.com/a/22774872/5177017): "If you pass a string to res.send(), it automatically assumes a Content-Type of html. res.end(), however, simply calls node's underlying end() implementation on the response stream, so no assumptions are made for the Content-Type." – abagh0703 Aug 09 '16 at 18:47
  • 3
    Had to upvote this purely because I've never used `express` but from the title thought - one sends something, one ends something.... 'twas right. – Darren Bartrup-Cook Jul 24 '18 at 15:01
  • 3
    But what if you just use `res.send()` with nothing. Does this act like `res.end()`? – CMCDragonkai May 19 '20 at 12:33
  • 3
    See the difference at [below answer](https://stackoverflow.com/a/49242271/2273211) and [what happened under the hood](https://stackoverflow.com/a/54874227/2273211) – KunYu Tsai Jun 10 '20 at 06:02
  • 1
    **Downvoted as it’s misleading.** “`.end()` will end the response process.” Yes, and so does `.send()`. You phrased as if it doesn’t. – Константин Ван Nov 26 '22 at 17:04
  • @CMCDragonkai Yes. To be precise, [`response.send()` calls `response.end(undefined, undefined)`](https://github.com/expressjs/express/blob/8368dc178af16b91b576c4c1d135f701a0007e5d/lib/response.js#L227-L236), which is equivalent to `response.end()`. – Константин Ван Nov 26 '22 at 17:50
60

res.send() implements res.write, res.setHeaders and res.end:

  1. It checks the data you send and sets the correct response headers.
  2. Then it streams the data with res.write.
  3. Finally, it uses 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.

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
Amit Wagner
  • 3,134
  • 3
  • 19
  • 35
9

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:

damd
  • 6,116
  • 7
  • 48
  • 77
Aalexander
  • 4,987
  • 3
  • 11
  • 34
7

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()

Nikhil Saini
  • 91
  • 3
  • 6
5

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

Pylon
  • 698
  • 6
  • 9