sudheeshcm
sudheeshcm

Reputation: 3418

What is the difference between res.end() and res.send()?

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?

Upvotes: 280

Views: 218763

Answers (6)

Aalexander
Aalexander

Reputation: 5004

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:

Upvotes: 11

Jorge González
Jorge González

Reputation: 3148

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

Upvotes: 304

itzmebibin
itzmebibin

Reputation: 9439

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.

Upvotes: 209

Nikhil Saini
Nikhil Saini

Reputation: 121

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

Upvotes: 10

Amit Wagner
Amit Wagner

Reputation: 3264

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.

Upvotes: 65

Pylon
Pylon

Reputation: 768

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

Upvotes: 5

Related Questions