resjend()在node.js中被快速调用? [英] Must res.end() be called in express with node.js?

查看:124
本文介绍了resjend()在node.js中被快速调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 Express 应用程序,我看到在某些模块中, res.end() 在请求处理程序的末尾调用(在 res.send res.json ),而其他则不调用。

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

例如:

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

或:

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

这两种情况都可以工作,但我害怕泄漏或运行文件描述符或类似的东西,当我运行很多请求哪一个是更正确?

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"?

推荐答案

你的问题的答案是否定的。如果调用 res.send(),则不需要调用 res.end() res.send()为您调用 res.end()

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.

取自 /lib/response.js ,这里是 res.send()函数的结尾:

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

  //. . .
  // respond
  this.end(head ? null : body);
  return this;
}

这篇关于resjend()在node.js中被快速调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆