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

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("https://stackoverflow.com/",(req,res)=>{
       res.send('<b>hello</b>');
    });

enter image description here


     app.get("https://stackoverflow.com/",(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("https://stackoverflow.com/",(req,res)=>{
           res.end('<b>hello</b>');
      }); 

enter image description here

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

      app.get("https://stackoverflow.com/",(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

Leave a Comment