greuze greuze - 1 year ago 88
Node.js Question

Must res.end() be called in express with node.js?

I have several Express applications, and I see that in some modules,

is called at the end of a request handler (after
), while in others, it isn't called.

For example:

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


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

Both cases work, but I'm afraid about leaks or running out file descriptors or something like that, when I run many requests. Which one is "more correct"?

Answer Source

The answer to your question is no. You don't have 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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download