Should res.end () be called in an expression using node.js?

I have several Express applications, and I see that in some modules res.end() is called at the end of the request handler (after res.send or res.json ), while in others it is not called.

For example:

 app.get('/test', function(req, res) { res.send('Test', 200); }); 

or

 app.get('/test', function(req, res) { res.send('Test', 200); res.end(); }); 

Both cases work, but I'm afraid to leak or end file descriptors or something like that when I run a lot of requests. Which one is "more correct"?

+69
express
Dec 03 '13 at 15:29
source share
3 answers

The answer to your question is no. You do not need to call res.end() if you call res.send() . res.send() calls res.end() for you.

Taken from /lib/response.js , here is the end of the res.send() function:

  //. . . // respond this.end(head ? null : body); return this; } 
+88
Dec 03 '13 at 15:35
source share

res.end([data] [, encoding])

Ends the response process. This method actually comes from Node core , specifically the response.end() method of http.ServerResponse. Use to quickly complete a response without any data.

If you need to respond with data, use methods such as res.send() and res.json().

+1
Oct 30 '16 at 5:39
source share

one example where you should call the end () function is when you send a buffer as a download file.

 res.write(buffer); res.end(); 
+1
Feb 03 '17 at 19:48
source share



All Articles