233

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,470
  • 4
  • 34
  • 59
sudheeshcm
  • 2,900
  • 3
  • 13
  • 20

7 Answers7

219

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

Patch92
  • 944
  • 9
  • 11
Jorge González
  • 2,298
  • 1
  • 7
  • 12
  • 18
    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
  • 2
    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
187

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.

Sid24
  • 324
  • 2
  • 13
itzmebibin
  • 8,738
  • 7
  • 46
  • 59
  • 92
    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
  • 42
    @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
  • 2
    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
  • 1
    But what if you just use `res.send()` with nothing. Does this act like `res.end()`? – CMCDragonkai May 19 '20 at 12:33
  • 2
    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
43

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
  • 3,651
  • 3
  • 16
  • 39
Amit Wagner
  • 2,831
  • 3
  • 15
  • 31
6

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
  • 81
  • 3
  • 6
6

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 really 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

Aalexander
  • 4,893
  • 3
  • 10
  • 31
6

res.end() function is used to end the response process. res.send() function is the combination of res.write(), res.setHeaders() and res.end().

Minhaz
  • 88
  • 2
  • 8
3

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
  • 493
  • 4
  • 8